您好我正在创建一个checklistbox
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox x:Name="CheckBox" Content="{Binding Path=Info.Name}" IsChecked="{Binding IsChecked,Mode=TwoWay}" Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainViewModel}}, Path=SimpleCommand}">
</CheckBox>
<TextBlock Margin="20,0,0,0" Text="{Binding Path=Status}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
但是当我检查simplecommand不是调用
SimpleCommand = new RelayCommand(obj => MessageBox.Show("alert"), obj => true);
答案 0 :(得分:0)
使用控件名称而不是视图模型作为祖先类型
<CheckBox x:Name="CheckBox"
Content="{Binding Path=Info.Name}"
IsChecked="{Binding IsChecked,Mode=TwoWay}"
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=SimpleCommand}"/>