Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 数据触发器绑定未按预期工作_Wpf_Xaml_Data Binding_Wpf Controls - Fatal编程技术网

Wpf 数据触发器绑定未按预期工作

Wpf 数据触发器绑定未按预期工作,wpf,xaml,data-binding,wpf-controls,Wpf,Xaml,Data Binding,Wpf Controls,我有一个为内容控件定义模板的样式 对于所有内容属性为null的控件,我希望显示文本,说明控件为空。。。但是下面的xaml不起作用,有人知道为什么吗 <Style TargetType="ContentControl" x:Key="style"> <Style.Triggers> <DataTrigger Binding="{Binding Pa

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

对于所有内容属性为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}" />

空的!

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

只需将textblock值从上下文之间更改为属性text=“empty”,如


以下工作:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">    
    <Window.Resources>
    <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>     
    </Window.Resources>
    <Grid>
        <ContentControl Content="{x:Null}" Style="{StaticResource style}" />
    </Grid>
</Window>

空的!

您的代码可以正常工作。将您的GUI设计器扔出窗口。

这没有什么区别,它完全等效,或者是空的,都是一样的。。。。两者都应该起作用。。。。我的问题是内容为空,并且没有调用controltemplate……。datatrigger条件不正确……它应该是正确的……这两个条件在设计和运行时都适用于我,我不知道为什么它在设计时不起作用。。。。只有在运行时…我尝试关闭和打开VS…窗口仍然是空的…是的,这种行为也发生在我身上。我建议您要么用手滚动您的XAML,要么使用混合。VS设计师留下了很多需要改进的地方。@devdigital:“留下了很多需要改进的地方”,这是一个非常好的委婉说法。@H.B.是的,它是唯一通过Sware过滤器的
<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">    
    <Window.Resources>
    <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>     
    </Window.Resources>
    <Grid>
        <ContentControl Content="{x:Null}" Style="{StaticResource style}" />
    </Grid>
</Window>