6

SQLでは、これを行うことができます:

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

Property1、2、および 3 がすべて null の場合、「すべて Null」となります

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>

結果は「Hello World」になるはずですが、代わりに「All Null」です

私の質問が明確であることを願っています。

4

3 に答える 3

10

それを行うには、カスタムIMultiValueConverterを構築し、MultiBindingを使用する必要があります。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>
于 2011-08-04T19:55:54.737 に答える
3

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

PriorityBindingのドキュメントの備考セクションでは、それがどのように機能するかについて詳しく説明しています。

于 2011-08-04T20:02:48.360 に答える
0

次へ進むためPriorityBindingだけに探しています。存在するので、それは設定され、はその値を取ります。DependencyProperty.UnsetValueBindingProperty1PriorityBinding

純粋な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>

ただし、これは少し複雑な方法であり、IMHOはUIではなくViewModelに属します。

于 2011-08-04T20:32:06.347 に答える