C# 将事件绑定到属性的备选方案
我有一个列表框,但如果我点击一个项目,我必须看到该项目的详细信息。我编写了这段代码,尝试将C# 将事件绑定到属性的备选方案,c#,mvvm,win-universal-app,commandbinding,event-binding,C#,Mvvm,Win Universal App,Commandbinding,Event Binding,我有一个列表框,但如果我点击一个项目,我必须看到该项目的详细信息。我编写了这段代码,尝试将SelectionChanged事件绑定到类型为RelayCommand的属性,模式为双向 <ListBox Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" SelectedItem="{Binding SelectedVillage, Mode=TwoWay}" ItemContainerStyle="{St
SelectionChanged
事件绑定到类型为RelayCommand
的属性,模式为双向
<ListBox Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3"
SelectedItem="{Binding SelectedVillage, Mode=TwoWay}"
ItemContainerStyle="{StaticResource lstidflt}"
SelectionChanged="{Binding SelectedVillageChanged, Mode=TwoWay}"
ItemTemplate="{StaticResource weatheritemdt}"
ItemsSource="{Binding VillageList}" />
这当然行不通,因为不能将事件绑定到属性或将属性绑定到方法,反之亦然。只能将属性绑定到属性。所以现在的问题是,是否有其他方法可以将SelectionChanged
事件绑定到属性
我在具有MVVM light体系结构的Windows universal 10应用程序中使用C#。您可以让SelectedItem属性的绑定
<ListBox ItemsSource="{Binding VillageList}" SelectedItem="{Binding SelectedVillage, Mode=TwoWay}" />
在二传手上做这项工作
public class VillageViewModel
{
public ObservableCollection<Village> VillageList { get; set; }
private Village selectedItem;
public Village SelectedItem
{
get { return selectedItem; }
set
{
if (selectedItem == value)
return;
selectedItem = value;
// Do logic on selection change.
}
}
}
公共类VillageViewModel
{
公共可见集合村民{get;set;}
私人村庄;
公营乡村
{
获取{return selectedItem;}
设置
{
如果(selectedItem==值)
返回;
选择editem=值;
//对选择进行逻辑更改。
}
}
}
我(在WPF中)所做的是将所选项目绑定到完整属性,然后在集合部分引发事件。它看起来像这样
private Village _SelectedVillage;
public Village SelectedVillage{
get {return _SelectedVillage;}
set {
_SelectedVillage = value;
RaiseEvent myEvent();
}
}
您还可以在xaml中启动relaycommand或检查触发器。如果使用该属性,请查看依赖项属性(如果在win 10 universal中可用)