2012-04-23 78 views
0

條件格式我有一些頭下面的模板:的頁眉內容

<telerik:RadExpander.Header> 
    <StackPanel Orientation="Horizontal" DataContext="{Binding CurrentItem, ElementName=activityProductGrid}"> 
     <TextBlock Text="{Binding Strings[Product], Source={StaticResource LanguageResources}, StringFormat='{}{0}: '}" /> 
     <TextBlock Text="{Binding Product.Name}" FontWeight="Bold" /> 
     <TextBlock Text="{Binding Strings[GroupName], Source={StaticResource LanguageResources}, StringFormat=', {0}: '}" /> 
     <TextBlock Text="{Binding GroupName}" /> 
     <TextBlock Text="{Binding Strings[UnitPrice], Source={StaticResource LanguageResources}, StringFormat=', {0}: '}" /> 
     <TextBlock Text="{Binding UnitPrice}" /> 
    </StackPanel> 
</telerik:RadExpander.Header> 

如何,我可以做頭的條件formmatting,例如,如果產品== null,則頭應該不會顯示任何內容?

編輯:這一個工程。

<StackPanel Orientation="Horizontal" DataContext="{Binding CurrentItem, ElementName=activityProductGrid}"> 
    <i:Interaction.Triggers> 
     <ie:DataTrigger Binding="{Binding}" Value="{x:Null}"> 
      <ie:ChangePropertyAction TargetObject="{RelativeSource={RelativeSource AncestorType=StackPanel}}" PropertyName="Visibility"> 
       <ie:ChangePropertyAction.Value> 
        <Visibility>Collapsed</Visibility> 
       </ie:ChangePropertyAction.Value> 
      </ie:ChangePropertyAction> 
     </ie:DataTrigger> 
    </i:Interaction.Triggers> 

    <-- from this point the came code as above --> 
</StackPanel> 

回答

0

具體地爲空,則可以替換值指定綁定參數,或者在一個顯示綁定錯誤的情況下:

<TextBlock 
    Text="{Binding Product.Name, TargetNullValue=(empty), FallbackValue=(error)}"/> 

EDIT(從WPF Binding Cheatsheet摘自):我注意到你不只是想要一個特定的綁定具有不同的值,而是整個控件不被顯示。爲此,您可以使用樣式和觸發器,將DataTrigger綁定到Product屬性,如果它爲空,則設置Visibility

<telerik:RadExpander.Header> 
    <StackPanel> 
      <StackPanel.Style> 
       <Style TargetType="StackPanel"> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding Product}" Value="{x:Null}"> 
        <Setter Property="Visibility" Value="Collapsed"/> 
        </DataTrigger> 
       </Style.Triggers> 
       </Style> 
      </StackPanel.Style> 
     </StackPanel> 
</telerik:RadExpander.Header> 
+0

嗨Avner,我試過類似的東西,它不工作。我需要針對Silverlight和WPF的解決方案,並且我將編輯這篇文章,爲您提供我所嘗試的內容 – Goran 2012-04-23 19:49:36

相關問題