【发布时间】:2014-04-18 18:25:37
【问题描述】:
我正在尝试使用 ICommand 接口实现我的第一个 wpf 项目。当用户单击重置按钮时,我想取消选择列表框中的所有项目。但我不知道如何从 viewmodel 中的 ICommand 访问列表框。我正在为 ICommand 使用 DelegateCommand 模式。它显示消息,但无法访问 AbsenseCodeListbox。那么这样做的方法是什么?谢谢。
<Button Content="Reset" Grid.Column="2" Margin="0,10,20,5" Command="{Binding AbsenceResetCommand}"/>
...
<ListBox Name="AbsenseCodeListbox" ItemsSource="{Binding absenseCodeItems, Mode=OneWay}" Grid.Row="1" Grid.Column="1" Grid.RowSpan="5" Margin="20,0,20,5" >
</ListBox>
我的视图模型中的代码:
void AbsenceResetCommand_Execute(object arg)
{
MessageBox.Show("_AbsenceResetCommand command");
AbsenseCodeListbox.UnselectAll();//Can't do this way
}
【问题讨论】: