【问题标题】:WPF Binding to a method with parameter inside HierarchicalDataTemplateWPF 绑定到在 HierarchicalDataTemplate 中带有参数的方法
【发布时间】:2010-12-19 07:06:43
【问题描述】:

有没有办法将值绑定到从方法获得的文本块。例如,我将我的 Person 对象传递给 HierarchicalDataTemplate,从那里我可以访问它的 Weight 属性。现在假设我想获得火星的重量,我会调用 InMars 方法,该方法采用 int EarthWeight 参数。现在earthweight要从Person到Person了,怎么每次都设置这个参数?

【问题讨论】:

    标签: wpf binding datatemplate hierarchicaldatatemplate objectdataprovider


    【解决方案1】:

    最好的方法是使用转换器。

    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}}
    

    【讨论】:

      猜你喜欢
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-16
      • 2012-06-19
      • 2012-06-21
      • 1970-01-01
      • 2020-03-17
      相关资源
      最近更新 更多