gpt4 book ai didi

c# - 有没有办法将命令绑定(bind)到 ListView 或 ComboBox 选择?

转载 作者:行者123 更新时间:2023-11-30 16:46:09 25 4
gpt4 key购买 nike

我无法找到将命令绑定(bind)到我的 listviewcombobox 选择事件或使用 mvvm 的任何鼠标单击事件的方法.

<StackPanel Orientation="Horizontal" >
<ComboBox Name="cmbID" Width="150" Margin="10" Height="30" SelectedItem="{Binding CmbSelected,Mode=TwoWay}" DisplayMemberPath="ID" ItemsSource="{Binding MyStudent,Mode=TwoWay}"/>
<Button Name="btnGetDetail" Margin="10" Command="{Binding getDetails}" Content="Get Details" Height="30" Width="90"/>
<TextBox Name="tbName1" Width="90" Height="30" Text="{Binding ElementName=cmbID,Path= SelectedItem.Sub}"></TextBox>
</StackPanel>

xaml 中,命令属性不像我们得到的按钮那样出现。

最佳答案

如果您使用 MVVM 模式,要在 ListViewComboBox 中选择一个项目时执行一个方法,您所要做的就是将它放在您的 SelectedItem 二传手。使用您的示例,在您的 View 模型中,您必须具有如下属性:

private object _cmbSelected;
public object CmbSelected
{
get
{
return this._cmbSelected;
}
set
{
this._cmbSelected= value;
//Here you can put your method
NotifyPropertyChanged("CmbSelected");
}
}

当一个项目被选中时, setter 被调用,你可以在那里调用任何你想要的方法。在此示例中,将 object 更改为适当的类型。

关于c# - 有没有办法将命令绑定(bind)到 ListView 或 ComboBox 选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40905284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com