gpt4 book ai didi

.net - 带有 ListView 的 MVVM 按钮事件未获取所选项目

转载 作者:行者123 更新时间:2023-12-03 10:39:18 25 4
gpt4 key购买 nike

我正在尝试从我的 ListView 中获取 selectedItem。我在按钮上使用 MVVM 灯光工具包和 EventToCommand。

我的 listView 绑定(bind)到正确绑定(bind)的 ObservableCollection。这是 listView xaml:

  <ListView   Name="serverListView"
Grid.Row="3"
Grid.Column="0"
Grid.ColumnSpan="2"
ItemsSource="{Binding Servers}"
ItemTemplate="{StaticResource ServerList}"
SelectionMode="Single"
BorderThickness="0"/>

然后我有一个按钮,我正在使用带有 mvvm EventToCommand 的 Interaction.Triggers,我不确定 selectedItem 绑定(bind)是否正确。该事件通过中继命令(mvvm light toolkit)正确触发,但我每次都得到空值。

这是我的按钮 xaml;
<Button x:Name="LoadButton"
Content="Load Server"
Grid.Column="0"
Grid.Row="4"
Grid.ColumnSpan="2">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<mvvm:EventToCommand Command="{Binding ButtonClick, Mode=OneWay}"
CommandParameter="{Binding SelectedItem, ElementName=serverListView}"
MustToggleIsEnabledValue="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>

中继命令:
 this.ButtonClick = new RelayCommand<object>(new Action<object>(this.GetClickEvent));

最佳答案

摆脱命令参数绑定(bind),在 View 模型中创建 SelectedServer 属性,在 View 模型中创建不带参数的命令

<ListView   Name="serverListView"
Grid.Row="3"
Grid.Column="0"
Grid.ColumnSpan="2"
ItemsSource="{Binding Servers}"
ItemTemplate="{StaticResource ServerList}"
SelectedItem="{Binding SelectedServer}"
SelectionMode="Single"
BorderThickness="0"/>

<Button Command="{Binding MyCommand}" />

在您的 View 模型中,您拥有执行命令逻辑所需的所有信息。

关于.net - 带有 ListView 的 MVVM 按钮事件未获取所选项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11030641/

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