WPF绑定到法参数内HierarchicalDataTemplate
-
21-09-2019 - |
题
是否有任何方式的值绑定到从的方法获得的文本块。例如,我通过我的Person对象到HierarchicalDataTemplate,从那里我可以访问它的粗细属性。现在可以说,我想在火星上的重量,我会打电话的InMars方法,它INT EarthWeight的参数。现在earthweight是要因人而变,怎么能这个参数,可以每次设置?
解决方案
要做到这一点,最好的方法是用一个转换器。
public class WeightOnMarsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// value will be the persons weight
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException("This method should never be called");
}
}
然后你只需要设置的绑定。
<l:WeightOnMarsConverter x:key="weightOnMars" /> <-- Add this to the resources
{Binding Path=Weight, Converter={StaticResource weightOnMars}}
不隶属于 StackOverflow