2011-12-07 5 views
5

私は特性の異なる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}を使用する方が良いですか?

+0

でその型を使用します

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

! – SvenG

答えて

6

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

エラーではなく警告であるため、デバッグウィンドウで警告されます。彼らはあなたに可能性のある問題を警告していますが、無視すれば悪いことはありません。

本当に気になる場合は、おそらくテンプレート列を使用し、さまざまなオブジェクトタイプに対して異なるデータテンプレートを指定することができます。私も時々、私はまったく同じ問題に直面し、私はその質問に良い解決策を見て熱心だtypeof(value)を返すConverterを使用し、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> 
+0

出力ウィンドウの警告がパフォーマンスに悪影響を及ぼすかどうか知っていますか? – SvenG

+2

@SvenGそうは思わない。警告はデバッグモードでのみ表示され、リリースモードでは存在しません。ここに関連するSOの答えがあります:http://stackoverflow.com/a/2594600/302677 – Rachel

+0

その説明のために多くのありがとう! – SvenG

0

私は次のように希望:

IMultiValueConverterを実装するカスタム値コンバーターを書きます。このコンバータでは、入力値をチェックし、有効な値を選択してその値を戻すことができます。 XAMLで

はそのようMultiBindingのを追加します。

<MultiBinding Converter="{StaticResource ResourceKey=myMultiValueConverter}" ConverterParameter="SomeParameterIfNecessary"> 
    <Binding "ToTheFirstClass.PropertyA" /> 
    <Binding "ToTheSecondClass.PropertyB" /> 
</MultiBinding> 
+0

これは、どちらか一方のプロパティを表示することを前提としています。これが当てはまる場合は、代わりにPriorityBindingを使用しますが、プロパティがオブジェクトに見つからない場合は、実際には空の値が必要です。 – snurre

関連する問題