• ListBox mvvm 学习笔记


    1. ListBox  MvvM

    例子1. 简单的绑定,ItemsSource 绑定到一个实现了IEnumerable 的类上。一般该绑定都是双向的,所以优先考虑使用 ObservableCollection<T>

    的类。这样界面和后台数据就同步了。针对于ListBox 的控件,我们比较关心的是SelectedItem,在mvvm 中,为了解耦前端界面和后台的逻辑,我们采用

    如下的方式,SelectedItem 双向绑定到ViewModel上的一个公用属性上。同时,该公用属性要实现INotifyPropertyChanged 接口,例子如下:

    XAML: 

    <ListBox Name="lst_catalog" ItemsSource="{Binding Catalogs}" SelectedItem="{Binding SelectedCatalog, Mode=TwoWay}">
    </ListBox>

    class:

    public class ViewModel:InotifyPropertyChanged

    {  

    public string SelectedCatalog
    {
    get
    {
    return _selectedCatalog;
    }
    set
    {
    _selectedCatalog = value;
    UpdateItemSources();
    OnPropertyChanged();
    }
    }

      public event PropertyChangedEventHandler PropertyChanged;

      private void OnPropertyChanged()

      {

        if(PropertyChanged!=null)

        {

          PropertyChanged(this,new PropertyChangedEventArgs(""));

        }

      }

    }

    2. ListBox 中使用DisplayMemberPath 和Converter 来限制listBox 中显示的内容。例子如下:

    xaml:

    <ComboBox Margin="5,0" Grid.Column="2" SelectedItem="{Binding ParamValue.RoleOSTemplate, Mode=TwoWay, Converter={StaticResource SelectedOsConverter}}" >
                        <ComboBox.ItemsSource>
                            <MultiBinding Converter="{StaticResource OsFilter}">
                                <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}" Path="DataContext.OSList"/>
                                <Binding Path="ParamName"/>
                            </MultiBinding>
                        </ComboBox.ItemsSource>
                    </ComboBox>

    class:

    [selectedOSConverter]

    public class TargetOSConvert : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string osTemplate = value as string;
                return GeneralInfoViewModel.SingleInstance.OSList.Where(each => each.OSName==osTemplate).FirstOrDefault();
            }
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                OSMapping osm = value as OSMapping;
                return osm.OSName;
            }
        }

    [OSFilter]

    public class OSListConvert:IMultiValueConverter
       {
           public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
           {           
               ObservableCollection<OSMapping> allList = (ObservableCollection<OSMapping>)value[0];
               string roleName = (string)value[1];
               if (roleName == null)
                   return null;
               string osType = roleName.Contains("TSVDA") || roleName.Contains("DDC") || roleName.Contains("SF") ? "server" : "desktop";
               ObservableCollection<OSMapping> returnVal = new ObservableCollection<OSMapping>( allList.Where(each => each.OSType==osType).ToList()) ;
               return returnVal;           
           }
           public object[] ConvertBack(object value,Type[] targetType, object parameter, CultureInfo culture)
           {
               throw new NotSupportedException();
           }
       }

  • 相关阅读:
    CorelDRAW X8完全学习教程
    Linux环境编程:从应用到内核
    Excel 2010 VBA 入门与实战
    iOS 用GDataXMLNode创建和解析XML
    iOS 使用SBJSON创建和解析JSON
    [iOS]MD5加密字符串
    Read and Write NSArray, NSDictionary and NSSet to a File
    ASIHTTPRequest类库简介和使用说明
    ios 调用webservice整理
    数据库迁移(SQL SERVER导入数据到MySql)
  • 原文地址:https://www.cnblogs.com/kongshu-612/p/5428206.html
Copyright © 2020-2023  润新知