wpf 将 DataTrigger 应用于 GridViewColumn

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17682755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:13:40  来源:igfitidea点击:

Applying DataTrigger To GridViewColumn

wpfxamllistviewdatatrigger

提问by KeyboardFriendly

How can I apply a datatrigger to the following in a VS 2012 WPF app?

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

I have tried this: Error: Foreground is not accessible or recognized

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

                <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>

I want it to Display No Title Found in a different color

我希望它以不同的颜色显示未找到的标题

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

回答by LPL

You have to specify the class of Foreground, you have to omit DisplayMemberBindingand use e.g. a TextBlockin DataTemplateinstead:

您必须指定的类Foreground,您必须省略DisplayMemberBinding并使用例如 a TextBlockinDataTemplate代替:

<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>