在WPF中使用DataTrigger设置Grid.Column

时间:2013-09-27 14:40:51

标签: c# wpf xaml

我正在尝试使用datatrigger设置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>

1 个答案:

答案 0 :(得分:4)

这可能与Dependency Property Value Precedence有关。您可以明确设置Column,而ColumnSpan使用默认值1。