2011-08-04 1 views
4

:プロパティ1、2と3はすべてnullの場合等価XAMLバインディングでCoalesce()に?私はこれを行うことができますSQLでは

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

は、その後、私はXAMLでこれを行うにはどうすればよい 'すべてのNull'

を取得しますか?私は私の質問は明らかであると思います

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

結果は「Hello Worldの」あってはならないが、代わりに、それは「すべてのNull」

です:私は次のことを試してみましたが、運。

答えて

7

これを行うには、IMultiValueConverterというカスタムを作成し、マルチバインディングを使用する必要があります。 PriorityBindingは、値を正常に生成するコレクション内の最初のバインディングを使用します。あなたの場合、Property1バインディングは即座に解決されるため、使用されます。 Property1はnullなので、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

クラスの4番目のプロパティもそれを行うことができます。私は考えます。 –

2

はあなたがStringに結合されているので、nullがPriorityBindingの有効な値です。私はあなたのItemクラスのプロパティタイプが何であるか分かりませんが、Objectを使用してDependencyProperty.UnsetValueに設定すると、探している動作が得られます。

PriorityBindingドキュメントの備考のセクションで、その動作の詳細について説明しています。

+0

このソリューションの唯一の残念な点は、エンティティライブラリ内のプレゼンテーションアセンブリへの依存です。そうです、このサンプルでは文字列です。私はオブジェクトを作るのはオブジェクトがあまりにもゆるく(特にXAMLのために)なると思います;)あなたの答えをありがとう。 –

+0

ええ、その場合、私は間違いなく@ CodeNakedのソリューションに行きます。 –

0

PriorityBindingDependencyProperty.UnsetValueを探して次のBindingに進みます。 Property1が存在するので、PriorityBindingがその値をとっています。 、それはそれをやって少し複雑な方法だ

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

ものの、およびIMHO、UIではなくViewModelにに属していません:純粋なXAMLソリューションの

、この Styleは仕事を行います。

+0

ああ、より速く入力する必要があります... :) – XAMeLi

+0

あなたのクレジットに、それはすべてXAMLですか? –

関連する問題