Datagrid中的复选框

时间:2016-02-18 13:56:11

标签: c# wpf checkbox mvvm datagrid

问题

ViewModel内检查Checkbox时,我似乎无法在DataGrid内部启动方法。

每行中都有一个Checkbox,当选择/取消选择Checkbox时,我想在我的ViewModel中触发一个更新数据库的方法。< / p>

代码

我的XAML中有Datagrid

    <DataGrid x:Name="dg_chapters" ItemsSource="{Binding Chapters}" SelectedItem="{Binding Chapter}" SelectionMode="Single" Background="Transparent" BorderBrush="#e5e5e5" CellStyle="{StaticResource episodeDataGridCell}" RowHeaderWidth="0" GridLinesVisibility="None" HorizontalAlignment="Stretch" Grid.Row="4" VerticalAlignment="Stretch" AutoGenerateColumns="False" Grid.RowSpan="1" Grid.Column="1" Grid.ColumnSpan="7">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Description}" CanUserResize="False" ClipboardContentBinding="{x:Null}" Header="Chapter" Width="90"/>
            <DataGridTextColumn Binding="{Binding Appearance}" CanUserResize="False" ClipboardContentBinding="{x:Null}" Header="Player" Width="90"/>
            <DataGridTextColumn Binding="{Binding Channel}" CanUserResize="False" ClipboardContentBinding="{x:Null}" Header="Channel" Width="100"/>
            <DataGridTextColumn Binding="{Binding Mode}" ClipboardContentBinding="{x:Null}" Header="Mode" Width="80"/>
            <DataGridCheckBoxColumn Binding="{Binding Protected}" Header="Protected" Width="80"/>
        </DataGrid.Columns>
    </DataGrid>

正如您在底部看到的是我的Checkbox列。在我的ViewModel我尝试创建一个命令来处理Checkbox的选定/取消选择:

    public ICommand Protected
    {
        get { return new DelegateCommand<object>(Protected_Click); }
    }

然后我创建了Protected方法:

    private void Protected_Click(object obj)
    {
        //DOES NOT HIT.
    }

任何人都可以看到我出错的地方以及如何让这项功能发挥作用?

1 个答案:

答案 0 :(得分:0)

DataGridTemplateColumn中的DataGrid内部复选框

 <DataGrid Name="GatewayDataGrid" Width="auto" Height="auto"> 
     <DataGridTemplateColumn Header="SplitMark" Width="30" >
      <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
      <CheckBox x:Name="Chb" VerticalAlignment="Center" HorizontalAlignment="Center" IsChecked="{Binding IsSelected,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Click="Chb_Click"  HorizontalContentAlignment="Center" VerticalContentAlignment="Top"></CheckBox>
      </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
      <DataGridTextColumn Header="Name" Binding="{Binding Name}"> </DataGridTextColumn >
      <DataGridTextColumn Header="Department" Binding="{Binding Department}"> </DataGridTextColumn >
    </DataGrid>