C# 组合框数据绑定到selectedvalue事件
当用户在组合框中选择不同的值时,我想执行一些函数 我使用的是MVVM模式C# 组合框数据绑定到selectedvalue事件,c#,wpf,data-binding,mvvm,combobox,C#,Wpf,Data Binding,Mvvm,Combobox,当用户在组合框中选择不同的值时,我想执行一些函数 我使用的是MVVM模式 知道在这种情况下如何绑定数据吗?绑定到SelectedItem上的属性。 在属性的setter中,执行函数。 SelectedItem=“{Binding Path=SomeProperty}”绑定到SelectedItem上的属性。 在属性的setter中,执行函数。 SelectedItem=“{Binding Path=SomeProperty}”您需要绑定到SelectedValue、SelectedItem、或S
知道在这种情况下如何绑定数据吗?绑定到SelectedItem上的属性。 在属性的setter中,执行函数。
SelectedItem=“{Binding Path=SomeProperty}”绑定到SelectedItem上的属性。 在属性的setter中,执行函数。
SelectedItem=“{Binding Path=SomeProperty}”您需要绑定到
SelectedValue
、SelectedItem
、或SelectedIndex
,具体取决于您要执行的操作:
public class MyViewModel : INotifyPropertyChanged
{
public ObservableCollection<MyObj> MyCollection { get; private set; }
private MyObj _theItem;
public MyObj TheItem
{
get { return _theItem; }
set
{
if (Equals(value, _theItem)) return;
_theItem= value;
//Or however else you implement this...
OnPropertyChanged("TheItem");
//Do something here.... OR
//This will trigger a PropertyChangedEvent if you're subscribed internally.
}
}
private string _theValue;
public string TheValue
{
get { return _theValue; }
set
{
if (Equals(value, _theValue)) return;
_theValue= value;
OnPropertyChanged("TheValue");
}
}
private int _theIndex;
public int TheIndex
{
get { return _theIndex; }
set
{
if (Equals(value, _theIndex)) return;
_theIndex = value;
OnPropertyChanged("TheIndex");
}
}
}
public class MyObj
{
public string PropA { get; set; }
}
<!-- Realistically, you'd only want to bind to one of those -->
<!-- Most likely the SelectedItem for the best usage -->
<ItemsControl ItemsSource="{Binding Path=MyCollection}"
SelectedItem="{Binding Path=TheItem}"
SelectedValue="{Binding Path=TheValue}"
SelectedIndex="{Binding Path=TheIndex}"
/>
公共类MyViewModel:INotifyPropertyChanged
{
public observeCollection MyCollection{get;private set;}
私营企业;
公共博物馆
{
获取{return\u-item;}
设置
{
如果(等于(值,_item))返回;
_Iteem=价值;
//或者不管你如何实现这个。。。
房地产变更(“ITEEM”);
//在这里做点什么……或者
//如果您在内部订阅,这将触发PropertyChangedEvent。
}
}
私有字符串\u值;
公共字符串值
{
获取{return\u theValue;}
设置
{
如果(等于(值,_theValue))返回;
_价值=价值;
不动产变更(“价值”);
}
}
私人国际指数;
公共内部索引
{
获取{return\u theIndex;}
设置
{
如果(等于(值,指数))返回;
_指数=数值;
房地产变更(“指数”);
}
}
}
公共类MyObj
{
公共字符串PropA{get;set;}
}
您需要绑定到SelectedValue
、SelectedItem
或SelectedIndex
,具体取决于您要执行的操作:
public class MyViewModel : INotifyPropertyChanged
{
public ObservableCollection<MyObj> MyCollection { get; private set; }
private MyObj _theItem;
public MyObj TheItem
{
get { return _theItem; }
set
{
if (Equals(value, _theItem)) return;
_theItem= value;
//Or however else you implement this...
OnPropertyChanged("TheItem");
//Do something here.... OR
//This will trigger a PropertyChangedEvent if you're subscribed internally.
}
}
private string _theValue;
public string TheValue
{
get { return _theValue; }
set
{
if (Equals(value, _theValue)) return;
_theValue= value;
OnPropertyChanged("TheValue");
}
}
private int _theIndex;
public int TheIndex
{
get { return _theIndex; }
set
{
if (Equals(value, _theIndex)) return;
_theIndex = value;
OnPropertyChanged("TheIndex");
}
}
}
public class MyObj
{
public string PropA { get; set; }
}
<!-- Realistically, you'd only want to bind to one of those -->
<!-- Most likely the SelectedItem for the best usage -->
<ItemsControl ItemsSource="{Binding Path=MyCollection}"
SelectedItem="{Binding Path=TheItem}"
SelectedValue="{Binding Path=TheValue}"
SelectedIndex="{Binding Path=TheIndex}"
/>
公共类MyViewModel:INotifyPropertyChanged
{
public observeCollection MyCollection{get;private set;}
私营企业;
公共博物馆
{
获取{return\u-item;}
设置
{
如果(等于(值,_item))返回;
_Iteem=价值;
//或者不管你如何实现这个。。。
房地产变更(“ITEEM”);
//在这里做点什么……或者
//如果您在内部订阅,这将触发PropertyChangedEvent。
}
}
私有字符串\u值;
公共字符串值
{
获取{return\u theValue;}
设置
{
如果(等于(值,_theValue))返回;
_价值=价值;
不动产变更(“价值”);
}
}
私人国际指数;
公共内部索引
{
获取{return\u theIndex;}
设置
{
如果(等于(值,指数))返回;
_指数=数值;
房地产变更(“指数”);
}
}
}
公共类MyObj
{
公共字符串PropA{get;set;}
}