wpf - 数据触发器绑定(bind)未按预期工作

标签 wpf xaml data-binding wpf-controls

我有一个定义内容控件模板的样式。

对于所有内容属性为 null 的控件,我想显示说明该控件为空的文本...但是下面的 xaml 不起作用,有人知道为什么吗?

        <Style TargetType="ContentControl" x:Key="style">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Content, RelativeSource={RelativeSource Self}}" Value="{x:Null}">
                    <Setter Property="ContentControl.Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                    <TextBlock Background="Blue">EMPTY!</TextBlock>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

<ContentControl Content="{x:Null}" Style="{StaticResource style}" />

它没有向我显示文本“EMPTY!”。

最佳答案

你的代码可以工作。把你的 GUI 设计器扔到窗外。

关于wpf - 数据触发器绑定(bind)未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10917842/

相关文章:

c# - 如何禁用 WPF 窗口上的 HitTest ?

c# - 错误: exception of type 'System.NullReferenceException' occurred in exe

wpf - 如何从滚动查看器中的最后一项中删除额外的边距?

c# - WPF 有条件地启用键绑定(bind)

c# - WPF 的 GUI 想法,虽然不知道如何实现它

c# - 即使成功采用了路径,图像也不会显示-MVVM WPF

c# - XAML 文件中的不同基类

ASP.NET 中继器绑定(bind)列表<string>

c# - 我觉得自己像个管道厂里的老鼠。设计 WPF 绑定(bind)解决方案的策略?

c# - RX : ComboBox empty if bind to list<string> | ViewModelViewHost could not find a valid view for the view model