列表框中的复选框中未触发已检查事件

时间:2016-02-14 14:13:12

标签: c# xaml

您好我正在创建一个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);

1 个答案:

答案 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}"/>