【问题标题】:How to call a command from ViewModel in a ListView where the ItemsSource is an other model class如何在 ItemsSource 是其他模型类的 ListView 中从 ViewModel 调用命令
【发布时间】:2018-12-25 22:32:17
【问题描述】:

所以我得到了一个视图,其中一个有一个 listView。这个 ListView 有一个 ItemSource “Persons”,这个列表视图我有一些 CheckBox。

选中 CheckBox 时,我想从我的 ViewModel“RaceSimulatorViewModel”执行命令“CanStartRaceCheckCommand”。

但这不起作用,因为程序试图从我的类 Person 获取命令。

所以绑定有一些错误。

你能帮帮我吗?

我已经尝试过这个解决方案: WPF MVVM: EventTrigger is not working inside CheckBox

有 Xaml :

<ListView ItemsSource="{Binding Persons}" Grid.Row="1" Margin="40,100,367,46" Grid.RowSpan="3" Grid.ColumnSpan="2"  >
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Full Name"  DisplayMemberBinding="{Binding Path=Name}"/>
            <GridViewColumn Header="Participe to race" >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <!--<CheckBox IsChecked="{Binding Path=IsParticipateToRace}" Command="{Binding CanStartRaceCheckCommand, RelativeSource={RelativeSource AncestorType={x:Type vm:RaceSimulatorViewModel}, AncestorLevel=1}}"></CheckBox>-->
                        <!--<CheckBox Command="{Binding CanStartRaceCheckCommand}" CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}" />-->
                        <CheckBox IsChecked="{Binding Path=IsParticipateToRace}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

还有我的输出:System.Windows.Data 错误:40:BindingExpression 路径错误:在 'object' ''Person' (HashCode=43304686)' 上找不到 'CanStartRAceCheckCommand' 属性。绑定表达式:路径=CanStartRAceCheckCommand; DataItem='人' (HashCode=43304686);目标元素是'CheckBox'(名称='');目标属性是“命令”(输入“ICommand”)

【问题讨论】:

  • 我试过了: System.Windows.Data 错误:4:无法找到用于与引用'RelativeSource FindAncestor,AncestorType='RaceSimulator.ViewModel.RaceSimulatorViewModel',AncestorLevel='1'' 绑定的源。 BindingExpression:Path=DataContext.CanStartRaceCheckCommand;数据项=空;目标元素是'CheckBox'(名称='');目标属性是“Command”(输入“ICommand”)

标签: c# wpf xaml mvvm


【解决方案1】:

标记扩展“RelativeSource”用于绑定到 DataContext 之外的对象。与“StaticResource”扩展相比,它适用于“RelativeSource”Binding 属性而不是“源”属性。它可以用来遍历可视化树。请记住,您正在创建元素树。 'RelativeSource' 允许您遍历这棵树回到根元素,并通过传递目标元素的类型和相对树级别来选择一个已访问元素作为绑定源。这类似于在 Binding 对象上设置“源”属性,您可以在其中指定数据源。数据本身或该源上数据的路径由Binding 的“路径”属性设置。

您必须遍历树,直到找到公开正确数据的元素。在您的情况下,绑定所需的数据源是ListView(它在您的默认数据上下文之外——> 相对源)。 ListView通过“DataContext”公开数据RaceSimulatorViewModel

所以

Command="{Binding DataContext.CanStartRaceCheckCommand, RelativeSource={RelativeSource AncestorType={x:Type vm:RaceSimulatorViewModel}, AncestorLevel=1}}"

变成

Command="{Binding DataContext.CanStartRaceCheckCommand, RelativeSource={RelativeSource AncestorType=ListView}}"

通常,数据上下文由子元素继承。对于模板(ControlTemplateDataTemplate),这略有不同。正如模板名称所暗示的,ControlTemplate 用于描述控件,DataTemplate 用于描述数据(或如何可视化)。因此,ControlTemplate 的数据上下文是模板化控件,而 DataTemplate 的数据上下文是模板化数据(在您的情况下为 Person 数据对象)。这使得基于数据状态的视觉效果成为可能,例如'Person.Age' 将其绘制为红色。 ControlTemplate 相比之下基本上是基于控制状态,例如CheckBox.IsChecked? --> 更改其他控件的可见性。

【讨论】:

  • 谢谢你的效果很好,你的解释真的很有帮助
猜你喜欢
  • 2020-07-12
  • 1970-01-01
  • 1970-01-01
  • 2016-06-08
  • 2012-04-21
  • 1970-01-01
  • 2022-11-11
  • 2021-03-24
  • 1970-01-01
相关资源
最近更新 更多