2

コンテンツコントロールのテンプレートを定義するスタイルがあります。

contentプロパティが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!」というテキストは表示されません。

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 に答える