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();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构