【问题标题】:Data-Trigger Binding not working as expected数据触发器绑定未按预期工作
【发布时间】:2012-06-06 16:00:11
【问题描述】:

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

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

【问题讨论】:

    标签: wpf xaml data-binding wpf-controls


    【解决方案1】:

    你的代码有效。把你的那个 GUI 设计器扔到窗外。

    【讨论】:

      【解决方案2】:

      以下作品:

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

      【讨论】:

      • 我不知道为什么它在设计时不起作用....仅在运行时...我尝试关闭和打开 VS...窗口仍然是空白的....
      • 是的,这种行为也发生在我身上。我建议要么手动滚动您的 XAML,要么使用 Blend。 VS 设计师还有很多不足之处。
      • @devdigital:“还有很多不足之处”,这是一个很好的委婉说法。
      • @H.B.是的,它是唯一一个通过发誓过滤器的人
      【解决方案3】:

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

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

      【讨论】:

      • 这没什么区别,完全等价
      • EMpty 是相同的......两者都应该工作。 ...我的问题是内容为空并且没有调用controltemplate.......数据触发条件不正确......应该是真的......
      • 这两个条件在设计和运行时都适用于我
      猜你喜欢
      • 1970-01-01
      • 2013-07-19
      • 2013-01-29
      • 1970-01-01
      • 1970-01-01
      • 2019-04-23
      • 1970-01-01
      • 2011-03-11
      • 1970-01-01
      相关资源
      最近更新 更多