Expander의 Header를 Stretch
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>
Reference
이 문제에 관하여(Expander의 Header를 Stretch), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/ailen0ada/items/d53dcf91b59de53f8e8c텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)