简体   繁体   English

无法在wpf DataGrid WPF中选择多个行或列

[英]Unable to select multiple rows or columns in wpf DataGrid WPF

I am making a chat app in which I have used Datagrid to show chat history. 我正在制作一个聊天应用程序,其中使用了Datagrid来显示聊天记录。 Now I am trying to select multiple messages at same time, just like any other chatting app. 现在,我尝试同时选择多个消息,就像其他聊天应用程序一样。 But I am unable to select more then one cell. 但是我无法选择一个以上的单元格。 Here is my XAML code. 这是我的XAML代码。 I have tried using different properties for selection but no use. 我尝试使用不同的属性进行选择,但没有用。

<DataGrid x:Name="MainChatDataGrid" Margin="0,0,0,0" ItemsSource="{Binding Source={StaticResource UserMessagesCollection}}"
                            IsReadOnly="True" AutoGenerateColumns="False" CanUserAddRows="False"
                            HeadersVisibility="None" GridLinesVisibility="None" 
                            BorderThickness="0" Background="White"
                            VerticalScrollBarVisibility="Auto" 
                            HorizontalScrollBarVisibility="Disabled"
                            SelectionMode="Extended"
                            SelectionUnit="CellOrRowHeader"
                            SelectiveScrollingGrid.SelectiveScrollingOrientation="Both"
                            CopyingRowClipboardContent="dataGrid1_CopyingRowClipboardContent"
                            HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                            CanUserResizeRows="False" >

                    <DataGrid.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate>
                                    <DockPanel Background="{x:Null}" >
                                        <Rectangle HorizontalAlignment="Stretch" Fill="#FFCCCCCC" Height="3"
                                                    DockPanel.Dock="Top" Margin="7.5,10,80,15" />
                                        <TextBlock Text="{Binding Path=Name}" Foreground="Black" IsEnabled="True"
                                                    DockPanel.Dock="Right" HorizontalAlignment="Right"
                                                    FontFamily="Arial" Margin="0,-25,0,0"/>
                                    </DockPanel>
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>
                        </GroupStyle>
                    </DataGrid.GroupStyle>
                    <DataGrid.RowStyle>
                        <Style TargetType="{x:Type DataGridRow}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsSenderLoggedInUser}" Value="True">
                                    <Setter Property="Background" Value="White" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding IsSenderLoggedInUser}" Value="False">
                                    <Setter Property="Background" Value="White" />
                                </DataTrigger>
                                <Trigger Property="IsSelected" Value="True" >
                                    <Setter Property="Background" Value="White" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </DataGrid.RowStyle>
                    <DataGrid.Resources>
                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF666666"/>
                    </DataGrid.Resources>
                    <DataGrid.HorizontalGridLinesBrush>
                        <SolidColorBrush Color="LightGray" />
                    </DataGrid.HorizontalGridLinesBrush>
                    <DataGrid.Columns >
                        <DataGridTemplateColumn Header="Image" >
                            <DataGridTemplateColumn.CellStyle>
                                <Style TargetType="DataGridCell">
                                    <Setter Property="Background" Value="{StaticResource columnBackgroundColor}"/>
                                    <Setter Property="BorderBrush" Value="{StaticResource columnBorderColor}" />
                                    <Setter Property="BorderThickness" Value="0" />
                                </Style>
                            </DataGridTemplateColumn.CellStyle>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>

                                    <Image Source="{Binding SenderImageSource}" Width="40" Height="40" 
                                               VerticalAlignment="Top" Margin="0,2,0,0" Stretch="Fill" >
                                        <Image.OpacityMask>
                                            <VisualBrush Visual="{Binding ElementName=Mask}" />
                                        </Image.OpacityMask>
                                    </Image>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTemplateColumn>
                            <DataGridTemplateColumn.CellStyle>
                                <Style TargetType="DataGridCell">
                                    <Setter Property="Background" Value="{StaticResource columnBackgroundColor}" />
                                    <Setter Property="BorderBrush" Value="{StaticResource columnBorderColor}" />
                                    <Setter Property="BorderThickness" Value="0" />
                                </Style>
                            </DataGridTemplateColumn.CellStyle>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBox  Margin="15,-1,0,0" Text="{Binding Sender}" 
                                        FontSize="12" VerticalAlignment="Top"
                                             IsEnabled="True"
                                             BorderThickness="0,0,0,0"
                                        FontWeight="Bold"
                                        Foreground="Black"
                                        IsReadOnly="True"
                                        HorizontalAlignment="Left"  />
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTemplateColumn Width="*">
                            <DataGridTemplateColumn.CellStyle>
                                <Style TargetType="DataGridCell">
                                    <Setter Property="Background" Value="{StaticResource columnBackgroundColor}" />
                                    <Setter Property="BorderBrush" Value="{StaticResource columnBorderColor}" />
                                    <Setter Property="BorderThickness" Value="0" />
                                </Style>
                            </DataGridTemplateColumn.CellStyle>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <local:RichTextBoxExt x:Name="rich" HorizontalAlignment="Left" SelectionBrush="Yellow" Margin="-55,15,0,0" IsReadOnly="True" BorderThickness="0,0,0,0" IsEnabled="True" IsUndoEnabled="False">
                                        <FlowDocument >
                                            <Paragraph>
                                                <Run Text="{Binding Message, Mode=OneWay}" />
                                            </Paragraph>
                                        </FlowDocument>
                                        <local:RichTextBoxExt.Emoticons>
                                            <local:EmoticonMapper Text="^_^" Icon="../Images/emoticons/01.png"/>
                                            <local:EmoticonMapper Text=":D" Icon="../Images/emoticons/02.png"/>
                                            <local:EmoticonMapper Text=":-D" Icon="../Images/emoticons/02.png"/>
                                            <local:EmoticonMapper Text=";)" Icon="../Images/emoticons/04.png"/>
                                            <local:EmoticonMapper Text=";-)" Icon="../Images/emoticons/04.png"/>
                                            <local:EmoticonMapper Text=":)" Icon="../Images/emoticons/05.png"/>
                                            <local:EmoticonMapper Text=":-)" Icon="../Images/emoticons/05.png"/>
                                            <local:EmoticonMapper Text="8)" Icon="../Images/emoticons/07.png"/>
                                            <local:EmoticonMapper Text="8-)" Icon="../Images/emoticons/07.png"/>
                                            <local:EmoticonMapper Text=":p" Icon="../Images/emoticons/08.png"/>
                                            <local:EmoticonMapper Text=":-p" Icon="../Images/emoticons/08.png"/>
                                            <local:EmoticonMapper Text=":o" Icon="../Images/emoticons/10.png"/>
                                            <local:EmoticonMapper Text=":-o" Icon="../Images/emoticons/10.png"/>
                                            <local:EmoticonMapper Text=":(" Icon="../Images/emoticons/12.png"/>
                                            <local:EmoticonMapper Text=":-(" Icon="../Images/emoticons/12.png"/>
                                            <local:EmoticonMapper Text=":'(" Icon="../Images/emoticons/13.png"/>
                                            <local:EmoticonMapper Text=":'-(" Icon="../Images/emoticons/13.png"/>
                                            <local:EmoticonMapper Text=":@" Icon="../Images/emoticons/14.png"/>
                                            <local:EmoticonMapper Text=":-@" Icon="../Images/emoticons/14.png"/>
                                            <local:EmoticonMapper Text=">:@" Icon="../Images/emoticons/14.png"/>
                                            <local:EmoticonMapper Text=">:-@" Icon="../Images/emoticons/14.png"/>
                                        </local:RichTextBoxExt.Emoticons>
                                    </local:RichTextBoxExt>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTemplateColumn>
                            <DataGridTemplateColumn.CellStyle>
                                <Style TargetType="DataGridCell">
                                    <Setter Property="Background" Value="{StaticResource columnBackgroundColor}"/>
                                    <Setter Property="BorderBrush" Value="{StaticResource columnBorderColor}" />
                                    <Setter Property="BorderThickness" Value="0" />
                                </Style>
                            </DataGridTemplateColumn.CellStyle>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBox Text="{Binding FormattedTimeStamp,Mode= OneWay}" Foreground="Black" IsEnabled="True" IsReadOnly="True" BorderThickness="0,0,0,0" FontSize="10" Width="45" VerticalAlignment="Top" HorizontalAlignment="Left" 
                                        Margin="0,-1,0,0"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>

Here is my screen shot of a selected cell 这是我所选单元格的屏幕截图 聊天应用

You need to set SelectionMode in you DataGrid. 您需要在DataGrid中设置SelectionMode

Set the SelectionMode to specify whether users can select multiple rows or only a single row at a time. 设置SelectionMode以指定用户一次可以选择多行还是只能选择单行。

DataGrid.SelectionMode DataGrid.SelectionMode

<DataGrid SelectionMode="Extended"

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

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