2

我有一种样式,它定义了内容控件的模板。

对于所有内容属性为空的控件,我想显示文本说控件是空的......但是下面的 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!”。

4

3 回答 3

2

Your code works. Take that GUI designer of yours and throw it out the window.

于 2012-06-06T16:14:26.713 回答
1

以下作品:

<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>
于 2012-06-06T16:10:03.893 回答
-1

只需将您的文本块值从上下文之间更改为属性 text="empty" 就像

  <TextBlock Background="Blue" Text="Empty"></TextBlock>

在此处输入图像描述

于 2012-06-06T16:05:53.670 回答