简体   繁体   English

具有CheckboxColumn的DataGrid不注册SelectedItem

[英]DataGrid with a CheckboxColumn does not register SelectedItem

I have a DataGrid in WPF. 我在WPF中有一个DataGrid。 It binds using MVVM to a ViewModel. 它使用MVVM绑定到ViewModel。 It works fine with just DataGridTextColumns that display information regarding my List of objects that the DataGrid binds to. 它仅与DataGridTextColumns配合使用,可以显示有关我与DataGrid绑定的对象列表的信息。 Clicking a row selects it, and this works too, firing off a series of events that allow me to work in depth with the given row and derived information. 单击一行将其选中,它也可以工作,并引发一系列事件,这些事件使我能够深入研究给定的行和派生的信息。

I then added a DataGridCheckBoxColumn so I can select items and remove them from my List if needed. 然后,我添加了一个DataGridCheckBoxColumn,以便可以选择项目并将它们从列表中删除(如果需要)。 This, However, breaks everything. 但是,这破坏了一切。 Note, that ViewModel remains unchanged entirely and the SelectedItem event for the DataGrid does not seem to fire at all now. 请注意,ViewModel完全保持不变,DataGrid的SelectedItem事件现在似乎根本没有触发。 Can anyone help so I can still retain my old functionality but also retain my column of checkboxes that help the user quickly do what he/she needs to do? 任何人都可以提供帮助,以便我仍然可以保留我的旧功能,但还可以保留我的复选框列,以帮助用户快速执行他/她需要做的事情?

Relevant XAML: 相关XAML:

<DataGrid Name="ArticlesOverviewDataGrid" IsReadOnly="True" 
          ItemsSource="{Binding Project.Articles}"  IsSynchronizedWithCurrentItem="True" 
          Style="{StaticResource DataGridStyle}"  SelectionUnit="FullRow"
          AutoGenerateColumns="False"   SelectionMode="Single" 
          SelectedItem="{Binding SelectedArticle, Mode=TwoWay}">
    <DataGrid.Columns>
        <DataGridCheckBoxColumn Width="65" HeaderStyle="{StaticResource DgchStyle}" Binding="{Binding IsDuplicate, UpdateSourceTrigger=PropertyChanged}" Header="Duplicate" ElementStyle="{StaticResource DataGridCheckBoxColumnElementStyle}"/>
        <DataGridTextColumn Width="*" HeaderStyle="{StaticResource DgchStyle}" Binding="{Binding Name}" Header="Name" ElementStyle="{StaticResource DataGridTextColumnElementStyle}" />
        <DataGridTextColumn Width="*" HeaderStyle="{StaticResource DgchStyle}" Binding="{Binding Author}" Header="Author" ElementStyle="{StaticResource DataGridTextColumnElementStyle}" />
        <DataGridTextColumn Width="100" HeaderStyle="{StaticResource DgchStyle}" Binding="{Binding PublicationDate}" Header="Publication Date" ElementStyle="{StaticResource DataGridTextColumnElementStyle}"/>
    </DataGrid.Columns>
    <DataGrid.InputBindings>
        <MouseBinding Gesture="LeftDoubleClick" Command="{Binding ShowProject}"/>
    </DataGrid.InputBindings>
</DataGrid>

Again, it all works if I remove the checkboxcolumn, meaning it messes with selecting an Item in the DataGrid, but I cannot seem to find any relevant information on WHY it breaks or how to circumvent this issue. 同样,如果我删除checkboxcolumn,这一切都可行,这意味着在DataGrid中选择一个项目会很麻烦,但是我似乎找不到任何有关为什么中断或如何解决此问题的信息。

<DataGridCheckBoxColumn Width="65" HeaderStyle="{StaticResource DgchStyle}" Binding="{Binding IsDuplicate, UpdateSourceTrigger=PropertyChanged}" Header="Duplicate" ElementStyle="{StaticResource DataGridCheckBoxColumnElementStyle}"/>

Try to use a template column and set it's DataTemplate with a checkbox instead: 尝试使用模板列,并使用复选框将其设置为DataTemplate

        <DataGridTemplateColumn Width="65" HeaderStyle="{StaticResource DgchStyle}" Header="Duplicate" ElementStyle="{StaticResource DataGridCheckBoxColumnElementStyle}">
            <DataTemplate>
                <CheckBox IsChecked="{Binding IsDuplicate, UpdateSourceTrigger=PropertyChanged}" />
            </DataTemplate>
        </DataGridTemplateColumn>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM