Wpf 使用DisplayMemberPath时更新组合框
我使用的是WPF和MVVM轻型框架(我对使用它们是新手) 情况如下:Wpf 使用DisplayMemberPath时更新组合框,wpf,combobox,mvvm-light,Wpf,Combobox,Mvvm Light,我使用的是WPF和MVVM轻型框架(我对使用它们是新手) 情况如下: 我有一个显示项目列表的组合框(从数据库加载),我正在使用DisplayMemberPath显示组合框中项目的标题 在同一GUI中,用户可以修改文本框中的项目标题。“保存”按钮允许用户将数据保存到数据库中 我想做的是,当用户单击“保存”时,组合框中的项目标题也会更新,此时会显示新值。但是,我不知道怎么做 有关我的实施的一些细节: MainWindow.xaml <ComboBox ItemsSource="{Bindi
- 我有一个显示项目列表的组合框(从数据库加载),我正在使用DisplayMemberPath显示组合框中项目的标题
- 在同一GUI中,用户可以修改文本框中的项目标题。“保存”按钮允许用户将数据保存到数据库中
<ComboBox ItemsSource="{Binding SourceData}" SelectedItem="{Binding SelectedSourceData,Mode=TwoWay}" DisplayMemberPath="Title" />
<TextBlock Text="{Binding SelectedDataInTextFormat}"/>
MainViewModel.xaml
public class MainViewModel:ViewModelBase
{
public ObservableCollection<Foo> SourceData{get;set;}
public Foo SelectedSourceData
{
get{return _selectedFoo;}
set{_selectedFoo=value; RaisePropertyChanged("SelectedSourceData"); }
}
public string SelectedDataInTextFormat
{
get{return _selectedDataInTextFormat;}
set{_selectedDataInTextFormat=value; RaisePropertyChanged("SelectedDataInTextFormat");
}
}
public类MainViewModel:ViewModelBase
{
公共ObservableCollection源数据{get;set;}
public Foo SelectedSourceData
{
获取{return\u selectedFoo;}
设置{u selectedFoo=value;RaisePropertyChanged(“SelectedSourceData”);}
}
公共字符串SelectedDataInTextFormat
{
获取{return\u selectedDataInTextFormat;}
设置{u selectedDataInTextFormat=value;RaisePropertyChanged(“selectedDataInTextFormat”);
}
}
如果有人能在这件事上帮助我,我将不胜感激
谢谢你的帮助
Romain当
SelectedDataInTextFormat
发生更改时,您只需更新SelectedSourceData
属性即可:
public string SelectedDataInTextFormat
{
get { return _selectedDataInTextFormat; }
set
{
_selectedDataInTextFormat = value;
RaisePropertyChanged("SelectedDataInTextFormat");
SelectedSourceData = SourceData.FirstOrDefault(f => f.Title == _selectedDataInTextFormat)
}
}
编辑:要更改组合框中当前选定的
Foo
项的Title
属性,可以在Foo类中实现:
public class Foo : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string title = string.Empty;
public string Title
{
get { return title; }
set
{
title = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Title"));
}
}
}
}
然后只需设置所选项目的Title
属性:
SelectedSourceData.Title = SelectedDataInTextFormat;
当
SelectedDataInTextFormat
更改时,您可以简单地更新SelectedSourceData
属性:
public string SelectedDataInTextFormat
{
get { return _selectedDataInTextFormat; }
set
{
_selectedDataInTextFormat = value;
RaisePropertyChanged("SelectedDataInTextFormat");
SelectedSourceData = SourceData.FirstOrDefault(f => f.Title == _selectedDataInTextFormat)
}
}
编辑:要更改组合框中当前选定的
Foo
项的Title
属性,可以在Foo类中实现:
public class Foo : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string title = string.Empty;
public string Title
{
get { return title; }
set
{
title = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Title"));
}
}
}
}
然后只需设置所选项目的Title
属性:
SelectedSourceData.Title = SelectedDataInTextFormat;
有很多方法可以做到这一点,本例利用
按钮标记属性将一些数据发送到保存按钮处理程序(或ICommand
),然后我们可以将文本框设置为Explicit
,并在单击按钮时调用更新
例如:
Xaml:
代码:
public分部类主窗口:窗口,INotifyPropertyChanged
{
私有ObservableCollection_sourceData=新ObservableCollection();
公共主窗口()
{
初始化组件();
Add(newfoo{Title=“Stack”});
Add(newfoo{Title=“Overflow”});
}
公共可观测收集源数据
{
获取{return\u sourceData;}
设置{u sourceData=value;}
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
var txtbx=(发送者为按钮)。标记为文本框;
GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
公共事件属性更改事件处理程序属性更改;
公共void RaisePropertyChanged(字符串属性)
{
if(PropertyChanged!=null)
{
PropertyChanged(此,新PropertyChangedEventArgs(property));
}
}
}
公共类Foo:INotifyPropertyChanged
{
私有字符串\u标题;
公共字符串标题
{
获取{return\u title;}
设置{u title=value;RaisePropertyChanged(“title”);}
}
公共事件属性更改事件处理程序属性更改;
公共void RaisePropertyChanged(字符串属性)
{
if(PropertyChanged!=null)
{
PropertyChanged(此,新PropertyChangedEventArgs(property));
}
}
}
有很多方法可以做到这一点,本例利用按钮标记属性将一些数据发送到保存按钮处理程序(或ICommand
),然后我们可以将文本框设置为Explicit
,并在单击按钮时调用更新
例如:
Xaml:
代码:
public分部类主窗口:窗口,INotifyPropertyChanged
{
私有ObservableCollection_sourceData=新ObservableCollection();
公共主窗口()
{
初始化组件();
Add(newfoo{Title=“Stack”});
Add(newfoo{Title=“Overflow”});
}
公共可观测收集源数据
{
获取{return\u sourceData;}
设置{u sourceData=value;}
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
var txtbx=(发送者为按钮)。标记为文本框;
GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
公共事件属性更改事件处理程序属性更改;
公共void RaisePropertyChanged(字符串属性)
{
if(PropertyChanged!=null)
{
PropertyChanged(此,新PropertyChangedEventArgs(property));
}
}
}
公共类Foo:INotifyPropertyChanged
{
私有字符串\u标题;
公共字符串标题
{
获取{return\u title;}
设置{u title=value;RaisePropertyChanged(“title”);}
}
公共事件属性更改事件处理程序属性更改;
公共void RaisePropertyChanged(字符串属性)
{
if(PropertyChanged!=null)
{
PropertyChanged(此,新PropertyChangedEventArgs(property));
}
}
}
代码:
public ObservableCollection<Foo> SourceData{get;set;}
public Foo SelectedSourceData
{
get{
return _selectedFoo;
}
set{
_selectedFoo=value;
RaisePropertyChanged("SelectedSourceData");
}
}
public string SelectedDataInTextFormat //Bind the text to the SelectedItem title
{
get{
return SelectedSourceData.Title
}
set{
SelectedSourceData.Title=value;
RaisePropertyChanged("SelectedDataInTextFormat");
}
}
publicobservableCollection源数据{get;set;}
public Foo SelectedSourceData
{
得到{
返回_selectedFoo;
}
设置{
_selectedFoo=值;
莱塞罗
<ComboBox ItemsSource="{Binding SourceData, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedSourceData,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Title" />
<TextBlock Text="{Binding SelectedDataInTextFormat, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>