wpf 扩展器,标题-停靠控制权

wh6knrhe  于 2023-03-31  发布在  其他
关注(0)|答案(1)|浏览(149)

我想让扩展器头中的红色按钮正确停靠。如何做到这一点?x1c 0d1x

<StackPanel Orientation="Vertical" x:Name="spTest" Margin="10">
    <Border BorderBrush="Black" BorderThickness="1">
        <Expander Margin="5,0,5,0">
            <Expander.Header>
                <Grid Margin="5,0,0,0" HorizontalAlignment="Stretch">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="40"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="TEXT1 1231521312"  Grid.Column="0" FontSize="18" Foreground="Black" VerticalAlignment="Center" HorizontalAlignment="Stretch"/>
                    <TextBlock Grid.Column="1" FontSize="18" FontStyle="Italic" Margin="10,0,10,0" Foreground="Black" VerticalAlignment="Center" Text="Text2"/>
                    <Button x:Name="btnRemove" Grid.Column="2" Width="40" Height="40" Background="#F8CECC" Content="X" FontWeight="Normal" FontSize="18" BorderBrush="#B95753"/>
                </Grid>
            </Expander.Header>
        </Expander>
    </Border>
</StackPanel>

谢谢你!

mzsu5hc0

mzsu5hc01#

这个问题来自于默认模板中用于标头的ContentPresenterContentPresenterHorizontalAlignment设置为不为Stretch。要更改它,您可以尝试将GridHorizontalAlignment绑定到ContentPresenterHorizontalAlignment

<StackPanel Orientation="Vertical" x:Name="spTest" Margin="10">
    <Border BorderBrush="Black" BorderThickness="1">
        <Expander Margin="5,0,5,0">
            <Expander.Header>
                <Grid Margin="5,0,0,0" 

                    HorizontalAlignment="{Binding HorizontalAlignment, RelativeSource={RelativeSource AncestorType=ContentPresenter}, Mode=OneWayToSource}">

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="40"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="TEXT1 1231521312"  Grid.Column="0" FontSize="18" Foreground="Black" VerticalAlignment="Center" HorizontalAlignment="Stretch"/>
                    <TextBlock Grid.Column="1" FontSize="18" FontStyle="Italic" Margin="10,0,10,0" Foreground="Black" VerticalAlignment="Center" Text="Text2"/>
                    <Button x:Name="btnRemove" Grid.Column="2" Width="40" Height="40" Background="#F8CECC" Content="X" FontWeight="Normal" FontSize="18" BorderBrush="#B95753"/>
                </Grid>
            </Expander.Header>
        </Expander>
    </Border>
</StackPanel>

另一种方法是设置/绑定Grid.Width,它可以处理嵌套的Expander

<Grid Margin="5,0,0,0" Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType=Expander}, Converter={StaticResource SubtractConverter}, ConverterParameter=5}">

public class SubtractConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return 
            (double)value 
            - 20/*Default template: Grid's column width */ 
            - 4/*Default template: margin of ContentPresenter*/ 
            - 2/*Default template:  2*BorderThikness */ 
            - double.Parse(parameter as string) /*Margin of Grid in Header*/;
    }

    public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture)
    {
        return value;
    }
}

另一种方法是覆盖Expander的默认模板,参见Expander Styles and Templates

相关问题