在 WPF 中使用 DataTrigger 设置 Grid.Column

本文关键字:设置 Grid Column DataTrigger WPF | 更新日期: 2023-09-27 18:32:59

我正在尝试使用数据触发器设置 Grid.Column 属性:

<Style TargetType="ListBox">
    <Style.Triggers>
        <DataTrigger Binding="{Binding View}" Value="HistoryLeft">
            <Setter Property="Visibility" Value="Hidden"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding View}" Value="HistoryRight">
            <Setter Property="Grid.Column" Value="0"/>
            <Setter Property="Grid.ColumnSpan" Value="2"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

Grid.ColumnSpan 工作正常,但 Grid.Column 未设置。有什么想法吗?

列表框的 XAML 是:

          <ListBox ItemsSource="{Binding HistoryItemsRight}" BorderThickness="0,1,0,0" Grid.Row="0" Grid.Column="1"  ItemsPanel="{StaticResource NoHorizontalScroll}" Background="White" ScrollViewer.VerticalScrollBarVisibility="Visible" 
                 HorizontalContentAlignment="Stretch"   ItemContainerStyle="{StaticResource HistoryItem}"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                    <ListBox.Style>
                        <Style TargetType="ListBox">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding View}" Value="HistoryLeft">
                                    <Setter Property="Visibility" Value="Hidden"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding View}" Value="HistoryRight">
                                    <Setter Property="Grid.Column" Value="0"/>
                                    <Setter Property="Grid.ColumnSpan" Value="2"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ListBox.Style>
                    <ListBox.Resources>
                        <ResourceDictionary>
                            <ResourceDictionary.MergedDictionaries>
                                <ResourceDictionary Source="/Resources/HistoryViewTemplates.xaml"/>
                                <ResourceDictionary>
                                    <DataTemplate DataType="{x:Type viewModel:HistoryItemView}">
                                        <Grid Style="{StaticResource HistoryItemGrid}">
                                            <Grid.ToolTip>
                                                <controls:HistoryViewToolTip></controls:HistoryViewToolTip>
                                            </Grid.ToolTip>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="30" MinWidth="30"/>
                                                <ColumnDefinition Width="60" MinWidth="50"/>
                                                <ColumnDefinition Width="250*" MinWidth="100"/>
                                            </Grid.ColumnDefinitions >
                                            <TextBlock  Text="{Binding Date, StringFormat=' HH:mm:ss'}" Grid.Column="1"/>
                                            <TextBlock  Text="{Binding Path}" Grid.Column="2" HorizontalAlignment="Stretch"/>
                                            <Button FontWeight="Bold" Padding="2,0,2,0" Margin="0,0,2,0" Width="19" Command="{Binding OperationToRight}" Grid.Column="0" Style="{StaticResource RightSideHistoryButton}" />
                                        </Grid>
                                    </DataTemplate>
                                </ResourceDictionary>
                            </ResourceDictionary.MergedDictionaries>
                        </ResourceDictionary>
                    </ListBox.Resources>
                </ListBox>

在 WPF 中使用 DataTrigger 设置 Grid.Column

这可能与依赖项属性值优先级有关。您可能显式设置了Column,而ColumnSpan使用默认值 1。