zoukankan      html  css  js  c++  java
  • 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.变量
    9.Mvvm结构,在后台CS中改变ViewModel中的值
     var viewModel = this.DataContext ;
          var mm = viewModel.GetType();
          viewModel.VisibleArea = (long)visibleArea;
    

     9.ContextMenu绑定

     <DataGrid.ContextMenu>
    <ContextMenu>
     <MenuItem Margin="0,0,0,5" Header="导出" Style="{x:Null}" Command="{Binding CmdItemDataGridExport}" 
     CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget}"
                                                       IsEnabled="{Binding EditUIEditable}" ></MenuItem>
      </ContextMenu>
     </DataGrid.ContextMenu>

      

     

  • 相关阅读:
    cachecloud搭建.md
    Python glob.md
    python textwrap.md
    Python shutil.md
    python openpyxl.md
    os.path.md
    Python datetime.md
    jenkins运行脚本 报错:UnicodeEncodeError: 'gbk' codec can't encode character 'xa9' in position 449: illegal multibyte sequence
    Python如何去实际提高工作的效率?也许这个会有用!
    Jmeter如何测试接口
  • 原文地址:https://www.cnblogs.com/mamaxiaoling/p/14435126.html
Copyright © 2011-2022 走看看