2011-08-04 39 views
4

在SQL我可以這樣做:Equiv。合併()在XAML綁定?

Select Coalesce(Property1, Property2, Property3, 'All Null') as Value 
From MyTable 

如果Property1,2和3均爲空,然後我得到 '全空'

如何在XAML做到這一點?我嘗試以下,但沒有運氣:

<Window.Resources> 
    <local:Item x:Key="MyData" 
       Property1="{x:Null}" 
       Property2="{x:Null}" 
       Property3="Hello World" /> 
</Window.Resources> 

<TextBlock DataContext="{StaticResource MyData}"> 
    <TextBlock.Text> 
     <PriorityBinding TargetNullValue="All Null"> 
      <Binding Path="Property1" /> 
      <Binding Path="Property2" /> 
      <Binding Path="Property3" /> 
     </PriorityBinding> 
    </TextBlock.Text> 
</TextBlock> 

結果應該是「世界你好」,而是它是「全空」

我希望我的問題是清楚的。

回答

7

你必須建立一個自定義IMultiValueConverter來做到這一點,並使用MultiBinding。 PriorityBinding使用成功生成值的集合中的第一個綁定。在你的情況下,Property1綁定立即解決,所以它被使用。由於Property1爲空,因此使用TargetNullValue。

像這樣的轉換器:

public class CoalesceConverter : System.Windows.Data.IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, 
      object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (values == null) 
      return null; 
     foreach (var item in values) 
      if (item != null) 
       return item; 
     return null; 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, 
      object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

而且MultiBinding這樣的:

<Window.Resources> 
    <local:Item x:Key="MyData" 
       Property1="{x:Null}" 
       Property2="{x:Null}" 
       Property3="Hello World" /> 
    <local:CoalesceConverter x:Key="MyConverter" /> 
</Window.Resources> 

<TextBlock DataContext="{StaticResource MyData}"> 
    <TextBlock.Text> 
     <MultiBinding Converter="{StaticResource MyConverter}"> 
      <Binding Path="Property1" /> 
      <Binding Path="Property2" /> 
      <Binding Path="Property3" /> 
     </MultiBinding> 
    </TextBlock.Text> 
</TextBlock> 
+1

我希望能在XAML中做到這一切 –

+0

@Jerry - 是的,一旦你有轉換器設置,你可以:-) – CodeNaked

+0

類的第四個屬性也可以做到這一點;我想。 –

2

因爲你是綁定到String,空是爲PriorityBinding的有效值。我不確定你的Item類的屬性類型是什麼,但如果你使用Object,並將它們設置爲DependencyProperty.UnsetValue,你將會得到你正在尋找的行爲。

PriorityBinding文檔的註釋部分描述了它如何更詳細地工作。

+0

該解決方案唯一不幸的部分是依賴於我的Entity Library中的Presentation組件。你是對的,他們是這個樣本中的字符串。我認爲讓它們對象太鬆散地鍵入我的對象(特別是爲了XAML);)謝謝你的回答。 –

+0

是的,在這種情況下,我一定會選擇@ CodeNaked的解決方案。 –

0

PriorityBinding只尋找DependencyProperty.UnsetValue前進到下一個Binding。由於Property1存在,因此設置它並且PriorityBinding取其值。

對於純XAML解決方案,這Style將做的工作:

<TextBlock> 
     <TextBlock.Style> 
      <Style TargetType="{x:Type TextBlock}"> 
       <Setter Property="Text" 
         Value="{Binding Property1}" /> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding Property1}" 
           Value="{x:Null}"> 
         <Setter Property="Text" 
           Value="{Binding Property2}" /> 
        </DataTrigger> 
        <MultiDataTrigger> 
         <MultiDataTrigger.Conditions> 
          <Condition Binding="{Binding Property1}" 
             Value="{x:Null}" /> 
          <Condition Binding="{Binding Property2}" 
             Value="{x:Null}" /> 
         </MultiDataTrigger.Conditions> 
         <Setter Property="Text" 
           Value="{Binding Property3}" /> 
        </MultiDataTrigger> 
        <MultiDataTrigger> 
         <MultiDataTrigger.Conditions> 
          <Condition Binding="{Binding Property1}" 
             Value="{x:Null}" /> 
          <Condition Binding="{Binding Property2}" 
             Value="{x:Null}" /> 
          <Condition Binding="{Binding Property3}" 
             Value="{x:Null}" /> 
         </MultiDataTrigger.Conditions> 
         <Setter Property="Text" 
           Value="All Null" /> 
        </MultiDataTrigger> 
       </Style.Triggers> 
      </Style> 
     </TextBlock.Style> 
    </TextBlock> 

雖然,這是做的有點令人費解的方式,恕我直言,在UI,但在視圖模型不屬於。

+0

噢,需要輸入更快...... :) – XAMeLi

+0

那麼,爲了您的信用,這是所有的XAML? –