当前位置:   article > 正文

WPF Convert和RelativeSource_binding relativesource converter

binding relativesource converter
自定义Converter

后台Converter类实现接口IValueConverter
方法Convert是值->UI
方法ConvertBack是UI->值
初始化走Convert

public class ColorConvert : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //value为当前的对象
        var item = value as ListViewItem;
        var view = ItemsControl.ItemsControlFromItemContainer(item);
        var index = view.ItemContainerGenerator.IndexFromContainer(item);

        var data = view.Items[index] as Student;
        if (data.Age == 22)
            return Brushes.Red;
        if (data.Age % 2 == 0)
            return Brushes.Pink;
        else
            return Brushes.DeepSkyBlue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
前端

前端引用命名空间,实例化静态资源

<converts:ColorConvert x:Key="MyConvert
    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/537650
    推荐阅读
    相关标签
      

    闽ICP备14008679号