阅读 72

WPF 绑定注意事项一

1.当一个界面不用控件需绑定不同ViewModel时,可在控件上单独设置DataContext

DataContext="{Binding Source={StaticResource Locator},Path=GB_FloCheckStepInforViewModel}"

2.如果该控件写在resource中,则无法读取DataType中字段值。即数据源头在一个控件上无法混用

3.MVVM在cs中使用Service中的方法时,注意实例化部分

private IGB_FloCheckStepInforService _service;
//实例化
_service = new XDS_SmartC.Services.GB_FloCheckStepInforService();
 var result = _service.FlowCheckmMultBack(curTempleteSource);

  

4.多字段绑定




 


5.多值转换器

 public class FlowCheckMultBackVisibleConvert : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {

            try
            {
                if (values[0].ToString().Contains("DependencyProperty.UnsetValue")
                    || values[1].ToString().Contains("DependencyProperty.UnsetValue")
                    )
                {
                    return Visibility.Collapsed;
                }

                string FlowMultBackRecord = values[0].ToString();
                int FlowCheckStatus = (int)values[1];

                if (FlowMultBackRecord != null && !FlowMultBackRecord.Trim().Equals("") && FlowCheckStatus == 1)
                {

                    return Visibility.Visible;
                }
                else
                {
                    return Visibility.Collapsed;
                }
                
            }
            catch (Exception)
            {
                return Visibility.Collapsed;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

 6.RelativeSource 绑定

//1.向上找到为button的控件的IsEnabled属性
"{Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}, Path= IsEnabled}"
//2.绑定自身的Header值
"{Binding RelativeSource={RelativeSource Self}, Path=Header}"

//3.魔板中的绑定:绑定setter中的属性
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"

//4. 绑定父级模板的属性
IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"

  7.DataTrigger触发的动态变化


  

 8.DataContext的绑定

//1.后台CS中绑定
DataContext = this;
//2.xaml的Style中想要绑定ViewModel中的值
需用DataContext.命令 或 DataContext.变量
3.Mvvm结构,在后台CS中改变ViewModel中的值
 var viewModel = this.DataContext ;
      var mm = viewModel.GetType();
      viewModel.VisibleArea = (long)visibleArea;

  

 

原文:https://www.cnblogs.com/mamaxiaoling/p/14435126.html

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐