私は特性の異なる2つのクラスを持っていますが、両方は他のいくつかの基本クラスを継承する:ソースオブジェクトが指定されたプロパティを持たない場合のバインディングの処理方法?
public class BaseClass { }
public class ClassA : BaseClass
{
public string PropertyA { get; set; }
}
public class ClassB : BaseClass
{
public string PropertyB { get; set; }
}
コードビハインド:
<ListView ItemsSource="{Binding Items}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding PropertyA, FallbackValue=''}"/>
<GridViewColumn DisplayMemberBinding="{Binding PropertyB, FallbackValue={x:Null}}"/>
</GridView>
</ListView.View>
</ListView>
:
public ObservableCollection<BaseClass> Items { get; set; }
public MainWindow()
{
Items = new ObservableCollection<BaseClass>
{
new ClassA {PropertyA = "A"},
new ClassB {PropertyB = "B"}
};
}
そして、私のXAMLは、このようになりますデバッグモードで実行すると、出力ウィンドウには次のように表示されます。
System.Windows.Data Warning: 40 : BindingExpression path error: 'PropertyB' property not found on 'object' ''ClassA' (HashCode=66437409)'. BindingExpression:Path=PropertyB; DataItem='ClassA' (HashCode=66437409); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Warning: 40 : BindingExpression path error: 'PropertyA' property not found on 'object' ''ClassB' (HashCode=2764078)'. BindingExpression:Path=PropertyA; DataItem='ClassB' (HashCode=2764078); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
これらのようなバインディングを処理するより良い方法はありますか?パフォーマンスに何か影響がありますか?FallbackValue = ''またはFallbackValue = {x:Null}を使用する方が良いですか?
でその型を使用します
! – SvenG