LoginSignup
8
5

More than 5 years have passed since last update.

ExpanderのHeaderをStretchさせる

Posted at

Problem

ExpanderコントロールのHeader, HeaderTemplateに書いた中身はStretchされません。

NotStretched.xaml
<Border BorderBrush="Black" BorderThickness="1" Margin="2">
    <Expander Margin="5">
        <Expander.Header>
            <Grid Background="PaleGreen">
                <TextBox Text="Not stretched." BorderBrush="Green" />
            </Grid>
        </Expander.Header>
    </Expander>
</Border>

Screen Shot 2014-04-18 at 14.31.46.png

なかなか解決策を思いつけなかったのでメモしておきます。
コードビハインドやビヘイビアを使わずにXAMLだけで解決したいところです。

Solution?

ActualWidthをバインドする方法。

ActualWidth.xaml
<Border BorderBrush="Black" BorderThickness="1" Margin="2">
    <Expander Margin="5">
        <Expander.Header>
            <Grid Background="GreenYellow"
              Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Expander}}, Path=ActualWidth}">
                <TextBox Text="Stretched?" BorderBrush="Indigo" />
            </Grid>
        </Expander.Header>
    </Expander>
</Border>

Screen Shot 2014-04-18 at 14.31.53.png

違う気がします。

Solution

HorizontalAlignmentをOneWayToSourceバインドする方法。

HorizontalAlignment.xaml
<Border BorderBrush="Black" BorderThickness="1" Margin="2">
    <Expander Margin="5">
        <Expander.Header>
            <Grid HorizontalAlignment="{Binding HorizontalAlignment, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Mode=OneWayToSource}">
                <TextBox Text="Stretched." BorderBrush="DeepPink" />
            </Grid>
        </Expander.Header>
    </Expander>
</Border>

Screen Shot 2014-04-18 at 14.31.58.png

欲しいのはこれでした。

Conclusion

要はHeaderのContentPresenterのHorizontalAlignmentがLeftで強制されているのが原因です。これに適切な値を入れることで回避できます。

参考にしたのはこちら。
Josh Smith on WPF

Sample

Screen Shot 2014-04-18 at 14.31.29.png

ExpanderTest.xaml
<Window x:Class="ExpanderTest.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" Height="200" Width="525">    <Grid>        <Grid.ColumnDefinitions>            <ColumnDefinition />            <ColumnDefinition />            <ColumnDefinition />        </Grid.ColumnDefinitions>        <Border Grid.Column="0" BorderBrush="Black" BorderThickness="1" Margin="2">            <Expander Margin="5">                <Expander.Header>                    <Grid Background="PaleGreen">                        <TextBox Text="Not stretched." BorderBrush="Green" />                    </Grid>                </Expander.Header>            </Expander>        </Border>        <Border Grid.Column="1" BorderBrush="Black" BorderThickness="1" Margin="2">            <Expander Margin="5">                <Expander.Header>                    <Grid Background="GreenYellow"                      Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Expander}}, Path=ActualWidth}">                        <TextBox Text="Stretched?" BorderBrush="Indigo" BorderThickness="1" />                    </Grid>                </Expander.Header>            </Expander>        </Border>        <Border Grid.Column="2" BorderBrush="Black" BorderThickness="1" Margin="2">            <Expander Margin="5">                <Expander.Header>                    <Grid HorizontalAlignment="{Binding HorizontalAlignment, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}}, Mode=OneWayToSource}">                        <TextBox Text="Stretched." BorderBrush="DeepPink" />                    </Grid>                </Expander.Header>            </Expander>        </Border>    </Grid></Window>
8
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5