码迷,mamicode.com
首页 > 其他好文 > 详细

ListBox mvvm 学习笔记

时间:2016-04-24 21:48:49      阅读:514      评论:0      收藏:0      [点我收藏+]

标签:

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();
       }
   }

ListBox mvvm 学习笔记

标签:

原文地址:http://www.cnblogs.com/kongshu-612/p/5428206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!