2016-09-30 10 views
0

私は、タイプMyPropertyClassMyPropertyという1つのプロパティを持つクラスMyClassを持っています。 MyPropertyClassはから変換するとimplicit operatorです。暗黙の変換が存在する場合、DataGridでクラスが自動的に変換されないのはなぜですか?

DataGridTextColumnのプロパティにバインドしたいが、これは機能しません。私の意見では、それはstringからMyPropertyClassに自動的に変換され、また戻る(ToStringメソッドを使用して)。

エラー:

System.Windows.Data Error: 1 : Cannot create default converter to perform 'two-way' conversions between types 'Test.MyPropertyClass' and 'System.String'. Consider using Converter property of Binding. BindingExpression:Path=MyProperty; DataItem='MyClass' (HashCode=22558296); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String')

私は状態上記のエラーの説明のようにConverterを定義することができますことを知っています。とにかくimplicit conversionからstringからMyPropertyClassまでの方法とToStringの方法しか使用しないので、これを行うのは冗長です。

コード:

class MyClass 
{ 
    public MyPropertyClass MyProperty { get; set; } 
} 

class MyPropertyClass 
{ 
    private string value; 

    public override string ToString() 
    { 
     return value; 
    } 

    public static implicit operator MyPropertyClass(string s) 
    { 
     MyPropertyClass mc = new MyPropertyClass(); 
     mc.value = s; 
     return mc; 
    } 
} 

XAML:

<DataGrid ItemsSource="{Binding List,Mode=OneWay}" AutoGenerateColumns="False"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="My Property" Binding="{Binding MyProperty,Mode=TwoWay}" /> 
    </DataGrid.Columns> 
</DataGrid> 

答えて

1

あなたはTypeConverterを探しています。それを指摘して

  1. CodeProject
  2. Wpf2000things
+0

おかげで、私はまだこれらのコンバータを書くことが冗長であると思います。私は、とにかく '暗黙の変換'と 'ToString'メソッドを使うつもりです。どうしてそれは自動的に自動的にできないのですか? – GregaMohorko

関連する問題