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