2012-06-06 29 views
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!」文本。

TKS, 威廉

回答

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

它在運行時工作.....它不應該在設計時工作? – will

+0

我不知道爲什麼它沒有在設計時間工作....只在運行時...我嘗試關閉並打開VS ...窗口仍然是空白的... – will

+0

是的,這種行爲也發生在我身上。我會建議您手動滾動您的XAML或使用Blend。 VS設計師留下了許多不足之處。 – devdigital

-1

簡單地從上下文之間改變你的文本塊值屬性text = 「空」 之類

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

enter image description here

+1

這是沒有區別的,它完全等價 – devdigital

+0

EMpty是一樣的....兩者都應該工作... 。我的問題是內容爲空,並且controltemplate沒有被調用....... datatrigger條件不是真的...並且它應該是真實的.... – will

+0

這兩個條件都適用於我在設計和運行時間 – JSJ

2

你的代碼有效。拿着你的GUI設計師,把它扔出窗外。