Expander의 Header를 Stretch

10308 단어 XamlWPF

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>



좀처럼 해결책을 떠올리지 않았기 때문에 메모해 둡니다.
코드 비하인드나 비헤이비어를 사용하지 않고 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>



다른 느낌.

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>



원하는 것은 이것이었습니다.

Conclusion



요점은 Header의 ContentPresenter의 HorizontalAlignment가 Left에서 강제되어 있기 때문입니다. 이것에 적절한 값을 넣는 것으로 회피할 수 있습니다.

참고로 한 것은 이쪽.
Josh Smith on WPF

샘플





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>

좋은 웹페이지 즐겨찾기