C# WPF组合框绑定以更新多个文本框
我有一节课C# WPF组合框绑定以更新多个文本框,c#,wpf,data-binding,C#,Wpf,Data Binding,我有一节课 class Names { public int id get; set;}; public string name {get ; set}; public string til {set{ if (this.name == "me"){ return "This is me"; } } 我有一个列表(ListNames),其中包含添加到列表中的名称,并使用组合框绑定 <ComboBox SelectedValue="{Binding Path=id, Mode=TwoWa
class Names {
public int id get; set;};
public string name {get ; set};
public string til {set{
if (this.name == "me"){
return "This is me";
}
}
我有一个列表(ListNames),其中包含添加到列表中的名称,并使用组合框绑定
<ComboBox SelectedValue="{Binding Path=id, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding
ListNames}" DisplayMemberPath="name" SelectedValuePath="id" />
一切正常
我想在用户选择项目时在另一个标签字段上显示“提示”
可能吗
救命啊 我假设您正在使用MVVM 您需要在窗口的viewmodel中创建一个类型为“Names”的属性“CurrentName”,绑定到组合框SelectedItem属性。 此属性必须引发NotifyPropertyChanged事件 然后,将标签字段绑定到此“CurrentName”属性。 当组合框上的SelectedItem属性发生更改时,您的标签字段将随之更新。类似以下内容: 您的型号:
public class Names
{
public int Id { get; set; }
public string Name { get; set; }
public string Tip {
get
{
return Name == "me" ? "this is me" : "";
}
}
}
您的ViewModel:
public class ViewModel : INotifyPropertyChanged
{
private ObservableCollection<Names> _namesList;
private Names _selectedName;
public ViewModel()
{
NamesList = new ObservableCollection<Names>(new List<Names>
{
new Names() {Id = 1, Name = "John"},
new Names() {Id = 2, Name = "Mary"}
});
}
public ObservableCollection<Names> NamesList
{
get { return _namesList; }
set
{
_namesList = value;
OnPropertyChanged();
}
}
public Names SelectedName
{
get { return _selectedName; }
set
{
_selectedName = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
公共类视图模型:INotifyPropertyChanged
{
私有可观测集合名称列表;
私人姓名_selectedName;
公共视图模型()
{
NamesList=新的ObservableCollection(新列表
{
新名称(){Id=1,Name=“John”},
新名称(){Id=2,Name=“Mary”}
});
}
公共可观测集合名称列表
{
获取{return\u namesList;}
设置
{
_名称列表=值;
OnPropertyChanged();
}
}
公共名称SelectedName
{
获取{return\u selectedName;}
设置
{
_selectedName=值;
OnPropertyChanged();
}
}
公共事件属性更改事件处理程序属性更改;
[NotifyPropertyChangedInvocator]
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
}
最后,你的观点是:
<Window x:Class="Combo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Combo="clr-namespace:Combo"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<Combo:ViewModel/>
</Window.DataContext>
<StackPanel>
<ComboBox ItemsSource="{Binding Path=NamesList}" DisplayMemberPath="Name"
SelectedValue="{Binding Path=SelectedName}"/>
<Label Content="{Binding Path=SelectedName.Name}"/>
</StackPanel>
你是说工具提示?还是名称类中的“til”属性?应该是小费吗?离题,但我要将您的类重命名为Name,公共属性应该以大写字母开头。您正在使用MVVM吗?在视图模型中实现
INotifyPropertyChanged
可以解决您的问题。我使用的是INotifyPropertyChanged,但不是MVVMLike@Miiite建议使用MVVM模式会使事情变得更简单。将标签文本绑定到ViewModel中的选定项。