【发布时间】: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”)