9

プロパティが異なる2つのクラスがありますが、どちらも他の基本クラスを継承しています。

public class BaseClass { }

public class ClassA : BaseClass
{
    public string PropertyA { get; set; }
}

public class ClassB : BaseClass
{
    public string PropertyB { get; set; }
}

コードビハインド:

public ObservableCollection<BaseClass> Items { get; set; }

public MainWindow()
{
    Items = new ObservableCollection<BaseClass>
        {
            new ClassA {PropertyA = "A"},
            new ClassB {PropertyB = "B"}
        };
}

そして、私のXAMLは次のようになります。

<ListView ItemsSource="{Binding Items}">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding PropertyA, FallbackValue=''}"/>
            <GridViewColumn DisplayMemberBinding="{Binding PropertyB, FallbackValue={x:Null}}"/>
        </GridView>
    </ListView.View>
</ListView>

デバッグモードで実行している場合、出力ウィンドウには次のように表示されます。

System.Windows.Data警告:40:BindingExpressionパスエラー:'PropertyB'プロパティが'オブジェクト''' ClassA'(HashCode = 66437409)'に見つかりません。BindingExpression:Path = PropertyB; DataItem ='ClassA'(HashCode = 66437409); ターゲット要素は'TextBlock'(Name ='');です。ターゲットプロパティは「テキスト」(タイプ「文字列」)です

System.Windows.Data警告:40:BindingExpressionパスエラー:'PropertyA'プロパティが'オブジェクト''' ClassB'(HashCode = 2764078)'に見つかりません。BindingExpression:Path = PropertyA; DataItem ='ClassB'(HashCode = 2764078); ターゲット要素は'TextBlock'(Name ='');です。ターゲットプロパティは「テキスト」(タイプ「文字列」)です

このようなバインディングを処理するためのより良い方法はありますか?パフォーマンスに影響はありますか?FallbackValue=''またはFallbackValue={x:Null}を使用する方が良いですか?

4

3 に答える 3

7

個人的には無視します。アイテムが存在しない場合、それは空の文字列として表示されます。これは通常、私が好むものです。

これらは単なる警告であり、エラーではないため、デバッグウィンドウの警告です。問題の可能性を警告していますが、無視しても問題は発生しません。

本当に気になる場合は、テンプレート列を使用して、オブジェクトタイプごとに異なるDataTemplatesを指定できます。

<DataTemplate TargetType="{x:Type local:ClassA}">
    <TextBlock Text="{Binding PropertyA}" />
</DataTemplate>

<DataTemplate TargetType="{x:Type local:ClassB}">
    <TextBlock Text="{Binding PropertyB}" />
</DataTemplate>

また、を返すコンバーターをtypeof(value)使用し、DataTriggerでそのタイプを使用することもあります

<Style.Triggers>
    <DataTrigger Value="{x:Type local:ClassA}" 
                 Binding="{Binding Converter={StaticResource ObjectToTypeConverter}}">
        <Setter Property="Text" Value="{Binding PropertyA}" />
    </DataTrigger>
    <DataTrigger Value="{x:Type local:ClassB}" 
                 Binding="{Binding Converter={StaticResource ObjectToTypeConverter}}">
        <Setter Property="Text" Value="{Binding PropertyB}" />
    </DataTrigger>
</Style.Triggers>
于 2011-12-07T15:26:03.233 に答える
2

この警告を排除する簡単なオプションはPriorityBinding、@snurreが次のように述べているように使用することです。

<GridViewColumn.DisplayMemberBinding>
    <PriorityBinding>
        <Binding Path="PropB" />
    </PriorityBinding>
</GridViewColumn.DisplayMemberBinding>

PropBプロパティが欠落している場合の警告は表示されません。

于 2018-09-27T07:22:15.037 に答える
0

私は次の方法を好みます:

を実装するカスタム値コンバーターを作成しますIMultiValueConverter。このコンバーターでは、入力値を確認し、有効な値を選択して、その値を返すことができます。

Xamlで、次のようなMultiBindingを追加します。

<MultiBinding Converter="{StaticResource ResourceKey=myMultiValueConverter}" ConverterParameter="SomeParameterIfNecessary">
    <Binding "ToTheFirstClass.PropertyA" />
    <Binding "ToTheSecondClass.PropertyB" />
</MultiBinding>
于 2011-12-07T12:23:23.020 に答える