其他分享
首页 > 其他分享> > WPF 绑定注意事项一

WPF 绑定注意事项一

作者:互联网

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

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

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

<DataTemplate DataType="{x:Type baseModel:FlowTempleteModel}">

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

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

  

4.多字段绑定

<Path.Visibility>
<MultiBinding Converter="{StaticResource FlowCheckMultBackVisibleConvert}">
<Binding Path="FlowMultBackRecord"/>
<Binding Path = "FlowCheckStatus"/> 
</MultiBinding>
</Path.Visibility>

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触发的动态变化

<Button.Resources>
  <Style TargetType="{x:Type Path}" >
   <Style.Triggers>
        <DataTrigger  Binding="{Binding  RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}, Path= IsEnabled}" Value="True">
          <Setter  Property="Fill" Value="{DynamicResource AccentColorBrush}" />
       </DataTrigger>
        <DataTrigger  Binding="{Binding  RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}, Path= IsEnabled}" Value="False">
          <Setter  Property="Fill" Value="LightGray" />
      </DataTrigger>
   </Style.Triggers>
</Style>
</Button.Resources>

 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;

  

 

标签:控件,DataContext,object,绑定,Binding,RelativeSource,注意事项,WPF
来源: https://www.cnblogs.com/mamaxiaoling/p/14435126.html