IM试图绑定方法的输出。现在我已经看到了使用的示例 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方法时,我无需操作

谢谢,斯蒂芬

有帮助吗?

解决方案

将呼叫调用在GET属性中,并将此获取属性添加到您当前DataContext的任何类中。

编辑:回答您的最新问题。

如果您只将一个参数传递给ValueConverter,则不需要多估计逆变器,只需使用常规的ValueConverter(实现IvalUeconverter)即可。另外,为什么不将ValueConverter中的对象施加到Distionary中并直接使用它而不是使用反射。

要通过绑定来传递当前数据台面:这样做: <Binding . />. 。我猜想TextBlock的DataContext是实体。

尽管如此,如果您要做的就是在访问字典项目之前运行一些代码,那么所有这些都是不必要的。只需使用索引属性,您就可以直接数据座上:

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]}" />
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top