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

转载于:https://www.cnblogs.com/kongshu-612/p/5428206.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值