質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

1回答

4165閲覧

WPF ScrollViewerに離接するオブジェクトに隙間ができる

Praline

総合スコア46

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

0クリップ

投稿2017/02/08 14:30

編集2017/02/09 13:23

###前提・実現したいこと
WPFでScrollViewerを設置すると、隣接するオブジェクトに隙間ができる。
マージンを引くことで隙間は削除できるのですが、ビューと実行結果が異なるため、どうにか解決できないでしょうか…

###発生している問題・エラーメッセージ

XML

1<Window x:Class="MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:Tw" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="350" Width="525"> 9 <Grid> 10 <ScrollViewer VerticalScrollBarVisibility="Auto" Margin="0,0,0,133" Background="Aqua"> 11 12 </ScrollViewer> 13 <StackPanel Background="AliceBlue" Margin="0,186,0,0"> 14 15 </StackPanel> 16 </Grid> 17</Window> 18

上記コードの場合、ビューワ上は問題ないのですが、実行するとScrollViewerとStackpanelの間に空白ができます。
イメージ説明

###追記

XML

1 2<Window x:Class="MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 7 xmlns:local="clr-namespace:Tw" 8 mc:Ignorable="d" 9 Title="MainWindow" Height="350" Width="525"> 10 <Grid> 11 12 <DockPanel> 13 <ScrollViewer VerticalScrollBarVisibility="Auto" Background="Aqua" Height="259" DockPanel.Dock="Top"> 14 15 </ScrollViewer> 16 <StackPanel Background="AliceBlue" Height="60" DockPanel.Dock="Bottom"> 17 <TextBox Height="40"></TextBox> 18 <Button Content="aaa"/> 19 </StackPanel> 20 21 </DockPanel> 22 23 </Grid> 24</Window>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Windowに対してWidthとHeightを指定されていますが、
デザイナで見るときはWindowStyleによって異なる外枠の幅が考慮されていないため、
コンテンツが確保できる領域が起動時とは若干異なります。

Windowではなく中のGridにWidthとHeightを指定し、
Windowには SizeToContent="WidthAndHeight" を指定するようにすれば
デザイナ上と差異がなくなると思います。

もう少し余計なことを言いますと、
そもそもGridは、ご提示のコードのように
Marginだけで位置を調整するような用途に向きません。

Gridらしくレイアウトするならば、以下のようになります。

XML

1<Window x:Class="MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:Tw" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="350" Width="525"> 9 <Grid> 10 <Grid.RowDefinitions> 11 <RowDefinition Height="186" /> 12 <RowDefinition Height="*" /> 13 </Grid.RowDefinitions> 14 15 <ScrollViewer Grid.Row="0" VerticalScrollBarVisibility="Auto" Background="Aqua"> 16 17 </ScrollViewer> 18 <StackPanel Grid.Row="1" Background="AliceBlue" > 19 20 </StackPanel> 21 </Grid> 22</Window>

Grid以外にもCanvas, StackPanel, DockPanelなどありますので
用途にあわせて選ぶと良いと思います。

投稿2017/02/08 15:02

oika

総合スコア425

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Praline

2017/02/09 13:24

dockpanelなどを使うとのことで追記させていただきました。 そもそもdockpanelを使うと、うまい具合に上下を配置できないのと、やはりscrollviewerが下のコントロールを押し下げているような動作になっています… 押し込むのはもう仕様として設計しなければいけないのでしょうか…?
Praline

2017/02/11 14:38

失礼しました、dockpanelの動作を把握していなかったのですが、scrollviewerとstackpanelを逆に並べると希望した動作になりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問