将DataTrigger应用于GridViewColumn

时间:2013-07-16 17:04:04

标签: wpf xaml listview datatrigger

如何在VS 2012 WPF应用程序中将数据触发器应用于以下内容?

我试过这个:错误:无法访问或识别前景

                <ListView.View>
                    <GridView AllowsColumnReorder="true"
                              ColumnHeaderToolTip="Information">
                        <GridViewColumn DisplayMemberBinding= "{Binding Path=Title , TargetNullValue='No Title Found'}" 
                                        Header="Title" Width="100">
                             <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <DataTemplate.Triggers>
                                        <DataTrigger Binding="{Binding Title}" Value="{x:Null}">
                                            <Setter Property="Foreground" Value="Salmon"/>
                                        </DataTrigger>
                                    </DataTemplate.Triggers>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                         </GridViewColumn>
                    </GridView>
                </ListView.View>

我希望它以不同的颜色显示无标题

                <DataTrigger Binding="{Binding Title}" Value="{x:Null}">
                    <Setter Property="Foreground" Value="Salmon"/>
                </DataTrigger>

1 个答案:

答案 0 :(得分:6)

您必须指定Foreground的班级,您必须省略DisplayMemberBinding并使用例如TextBlockDataTemplate中的<DataTemplate> <TextBlock Text="{Binding Path=Title , TargetNullValue='No Title Found'}"/> <DataTemplate.Triggers> <DataTrigger Binding="{Binding Title}" Value="{x:Null}"> <Setter Property="TextBlock.Foreground" Value="Salmon"/> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> 代替:

{{1}}