2

メソッドの出力にバインドしようとしています。これを使用した例を見てきましたが、これに関するObjectDataProvider問題は、ObjectDataProviderがメソッドを呼び出すオブジェクトの新しいインスタンスを作成することです。現在のオブジェクトインスタンスで呼び出されるメソッドが必要な場合。私は現在、コンバーターを動作させようとしています。

設定:

Class Entity
{
   private Dictionary<String, Object> properties;

   public object getProperty(string property)
  {
      //error checking and what not performed here
     return this.properties[property];
  }
}

XAMLでの私の試み

     <local:PropertyConverter x:Key="myPropertyConverter"/>
      <TextBlock Name="textBox2">
          <TextBlock.Text>
            <MultiBinding Converter="{StaticResource myPropertyConverter}"
                          ConverterParameter="Image" >
              <Binding Path="RelativeSource.Self" /> <!--this doesnt work-->
            </MultiBinding>
          </TextBlock.Text>
        </TextBlock>

背後にある私のコード

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    string param = (string)parameter;
    var methodInfo = values[0].GetType().GetMethod("getProperty", new Type[0]);
    if (methodInfo == null)
        return null;
    return methodInfo.Invoke(values[0], new string[] { param });               
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
    throw new NotSupportedException("PropertyConverter can only be used for one way conversion.");
}

私の問題は、現在のエンティティをコンバータに渡すことができないように見えることです。したがって、リフレクションを使用してgetPropertyメソッドを取得しようとすると、操作するものが何もありません。

ありがとう、ステフ

4

1 に答える 1

1

getプロパティ内のメソッドへの呼び出しをラップし、このgetプロパティを現在のDataContextであるクラスに追加します。

編集:更新された質問に答えます。

値コンバーターにパラメーターを1つだけ渡す場合は、複数値コンバーターは必要ありません。通常の値コンバーター(IValueConverterを実装)を使用するだけです。また、valueconverterのオブジェクトをDistionaryにキャストして、リフレクションを使用する代わりに直接使用してみませんか。

現在のデータコンテキストをバインディングとして渡すには、次のようにします<Binding . />。テキストブロックのデータコンテキストはエンティティだと思います。

それでも、辞書アイテムにアクセスする前にコードを実行するだけの場合は、これはすべて必要ありません。代わりにインデックスプロパティを使用するだけで、直接データバインドできます。

public class Entity 
{ 
   private Dictionary<String, Object> properties; 

   public object this[string property]
   {
        get
        { 
            //error checking and what not performed here 
            return properties[property]; 
        }
    } 
} 

<TextBlock Text="{Binding Path=[Image]}" />
于 2010-09-15T21:49:29.923 に答える