我有一个列表框,但是如果单击某个项目,则必须查看该项目的详细信息。我在尝试将SelectionChanged
事件绑定(bind)到属性类型为RelayCommand
的模式时编写了此代码,而mode是两种方式。
<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}" />
当然这是行不通的,因为您不能将事件绑定(bind)到属性,也不能将属性绑定(bind)到方法,反之亦然。您只能将属性绑定(bind)到属性。所以问题是,现在有替代方法可以将
SelectionChanged
事件绑定(bind)到属性吗?我在具有MVVM轻型体系结构的Windows通用10应用程序中使用C#。
最佳答案
您可以只绑定(bind)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.
}
}
}
关于c# - 将事件绑定(bind)到属性的替代方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34543526/