Im attempting to bind to the output of a method. Now I've seen examples of this using ObjectDataProvider
However the problem with this is ObjectDataProvider creates a new instance of the object to call the method. Where I need the method called on the current object instance. I'm currently trying to get a converter to work.
我试图绑定到方法的输出。现在我已经看到了使用ObjectDataProvider的例子,但是这个问题是ObjectDataProvider创建一个对象的新实例来调用这个方法。我需要在当前对象实例中调用该方法。我目前正在尝试让一个转换器工作。
Setup:
设置:
Class Entity
{
private Dictionary<String, Object> properties;
public object getProperty(string property)
{
//error checking and what not performed here
return this.properties[property];
}
}
My attempt at the XAML
我尝试了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>
my code behind
我的代码在
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.");
}
My problem is that I cant seem to pass the current Entity into the converter. So When i try to use reflection to get the getProperty method I have nothing to operate on
我的问题是我似乎不能将当前的实体传递给转换器。所以当我尝试使用反射来获取getProperty方法时,我没有任何操作。
thanks, steph
谢谢你,史蒂芬
1 个解决方案
#1
1
Wrap the call to the method inside a get property and add this get property to whatever class that is your current DataContext.
在get属性中封装对方法的调用,并将这个get属性添加到当前DataContext的任何类中。
Edit: Answering your updated question.
编辑:回答你的问题。
If you only pass one parameter to the valueconverter you don't need a multivalueconverter, just use a regular valueconverter (implementing IValueConverter). Also, why not cast the object in the valueconverter to a Distionary and use it directly instead of using reflection.
如果您只将一个参数传递给valueconverter,那么您不需要一个multivalueconverter,只需使用一个常规的valueconverter(实现IValueConverter)。同样,为什么不将valueconverter中的对象转换为一个蒸馏器,并直接使用它,而不是使用反射。
To pass current datacontext as a binding do this: <Binding . />
. I'm guessing the datacontext of the textblock is entity.
要将当前的datacontext作为绑定传递,请执行以下操作: <绑定。> 。我猜textblock的datacontext是entity。
Still, all this is not necessary if all you want to do is run some code before accessing a dictionary item. Just use an index property instead, you can databind to it directly:
不过,如果您只想在访问字典项之前运行一些代码,那么所有这些都不是必需的。只需使用一个索引属性,就可以直接对其进行数据库:
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]}" />
#1
1
Wrap the call to the method inside a get property and add this get property to whatever class that is your current DataContext.
在get属性中封装对方法的调用,并将这个get属性添加到当前DataContext的任何类中。
Edit: Answering your updated question.
编辑:回答你的问题。
If you only pass one parameter to the valueconverter you don't need a multivalueconverter, just use a regular valueconverter (implementing IValueConverter). Also, why not cast the object in the valueconverter to a Distionary and use it directly instead of using reflection.
如果您只将一个参数传递给valueconverter,那么您不需要一个multivalueconverter,只需使用一个常规的valueconverter(实现IValueConverter)。同样,为什么不将valueconverter中的对象转换为一个蒸馏器,并直接使用它,而不是使用反射。
To pass current datacontext as a binding do this: <Binding . />
. I'm guessing the datacontext of the textblock is entity.
要将当前的datacontext作为绑定传递,请执行以下操作: <绑定。> 。我猜textblock的datacontext是entity。
Still, all this is not necessary if all you want to do is run some code before accessing a dictionary item. Just use an index property instead, you can databind to it directly:
不过,如果您只想在访问字典项之前运行一些代码,那么所有这些都不是必需的。只需使用一个索引属性,就可以直接对其进行数据库:
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]}" />