DataGrid行选择不会触发属性更改

时间:2014-04-30 14:56:05

标签: wpf datagrid selecteditem propertychanged

我有一个数据网格,当我选择一行时,正确调用绑定到所选项目的属性,但如果我再次单击同一行(取消选中),则不会调用已更改的属性。 任何想法为什么? ItemsSource对象实现了INotifyPropertyChaqnged,VM中的属性是标准的,正如我所说的那样,当我取消选择一行时它不起作用。 这是我的XAML:

<DataGrid
     CanUserAddRows="False"
     CanUserDeleteRows="False"
     CanUserReorderColumns="False"
     GridLinesVisibility="None"
     ItemsSource="{Binding AllChannelsRecordingSchedule,UpdateSourceTrigger=PropertyChanged}"
     SelectedItem="{Binding SelectedChannelRecordingServer,UpdateSourceTrigger=PropertyChanged}"
     AlternatingRowBackground="{StaticResource VigilantDataGridAlternatingRowColor}"
     ColumnHeaderStyle="{StaticResource VigilantDataGridColumnHeader}"
     RowBackground="White"
     RowHeaderWidth="0"
     AutoGenerateColumns="False"
     FontFamily="Arial"
     FontSize="14"
     MaxWidth="805"
     Height="350"
     Background="White">
     <DataGrid.Resources>
          <Style TargetType="DataGridCell">
               <Setter Property="BorderThickness" Value="0"/>
               <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
          </Style>
     </DataGrid.Resources>
          <DataGrid.RowStyle>
              <Style TargetType="{x:Type DataGridRow}">
                  <Style.Triggers>
                       <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Background" Value="Transparent" />
                               <Setter Property="Foreground" Value="#000" />
                             </Trigger>
                             <Trigger Property="IsSelected" Value="true">
                                <Setter Property="Background" Value="Blue" />
                                <Setter Property="Foreground" Value="#000" />
                               </Trigger>
                            </Style.Triggers>
                        </Style>
                    </DataGrid.RowStyle>
                    <DataGrid.Columns>
                        <DataGridTextColumn
                            Header="{x:Static p:Resources.SystemRecordingSchedule_ServerName}"
                            Width="*"
                            IsReadOnly="True"
                            Binding="{Binding ServerName,UpdateSourceTrigger=PropertyChanged}">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="HorizontalAlignment" Value="Center" />
                                    <Setter Property="VerticalAlignment" Value="Center" />
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>

                        <DataGridTextColumn
                            Header="{x:Static p:Resources.SystemRecordingSchedule_ServerIP}"
                            MinWidth="120"
                            IsReadOnly="True"
                            Binding="{Binding ServerIP,UpdateSourceTrigger=PropertyChanged}">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="HorizontalAlignment" Value="Center" />
                                    <Setter Property="VerticalAlignment" Value="Center" />
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>

                        <DataGridTextColumn
                            Header="{x:Static p:Resources.SystemRecordingSchedule_ChannelName}"
                            MinWidth="150"
                            IsReadOnly="True"
                            Binding="{Binding ChannelName,UpdateSourceTrigger=PropertyChanged}">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="HorizontalAlignment" Value="Center" />
                                    <Setter Property="VerticalAlignment" Value="Center" />
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>

                        <DataGridTemplateColumn
                            Header="00:00       06:00       12:00       18:00       24:00"
                            MinWidth="350"
                            IsReadOnly="True">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <userControls:SchedulerControl
                                        FullRecordingSchedule="{Binding FullRecordingSchedule,UpdateSourceTrigger=PropertyChanged}"
                                        IsEnabled="False"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>

感谢

1 个答案:

答案 0 :(得分:1)

首先修复代码示例格式化 没有人想要向右看

您假设第二次单击取消选择无效

该属性为SelectedItem(未SelectedUnselectedItem)

第二次点击只是重新选择,因为它已被选中,所以UI不会激活

即使你有一个选择更改事件,它也不会在第二个选择中触发。

如果您需要选择/取消选择的内容,可以使用CheckBox列