WPF,如何获取数据网格中的选定文本

edqdpe6u  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(175)

我正在编辑datagrid中的一个文本单元格,我想获取datagrid单元格文本中的所选文本。是否有针对它的事件?目标是找出所选文本的含义,并显示带有该含义的工具提示。请参见图片
enter image description here

<DataGrid x:Name="dgMeanings" Grid.Row="5" FontSize="16"  AutoGenerateColumns="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RowEditEnding="dgMeanings_RowEditEnding" PreviewKeyDown="dgMeanings_PreviewKeyDown" AddingNewItem="dgMeanings_AddingNewItem" Background="{Binding ButtonBackColor}" Foreground="{Binding TextForeColor}" SelectionChanged="txt_SelectionChanged" CellEditEnding="dgMeanings_CellEditEnding" >
                        <DataGrid.Columns>
                            <DataGridTextColumn  Header="#"  Binding="{Binding IORDER,UpdateSourceTrigger=LostFocus}" Width="30"/>
                            <DataGridTextColumn  Header="LEVEL"  Binding="{Binding LEVEL,UpdateSourceTrigger=LostFocus}" Width="50"/>
                            <DataGridTextColumn  Header="AS"  Binding="{Binding TYPE ,UpdateSourceTrigger=LostFocus}" Width="60" />
                            <DataGridTextColumn  Header="MEANING"  Binding="{Binding MEANING ,UpdateSourceTrigger=LostFocus}" Width="400" />
                            <DataGridTextColumn  Header="TRANSLATION"  Binding="{Binding TRANSLATION ,UpdateSourceTrigger=LostFocus}" Width="150" />
                            <DataGridTextColumn  Header="EXAMPLE"  Binding="{Binding EXAMPLE,UpdateSourceTrigger=LostFocus}" Width="400"/>
                            <DataGridTextColumn  Header="EXAMPLE TRANSLATION"  Binding="{Binding EXAMPLE_TRANSLATION,UpdateSourceTrigger=LostFocus}" Width="400"/>
                        </DataGrid.Columns >
                        <DataGrid.Resources>
                            <Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" TargetType="{x:Type DataGridColumnHeader}">
                                <Setter Property="Background" Value="Black" />
                            </Style>
                        </DataGrid.Resources>
                        <DataGrid.RowStyle>
                            <Style TargetType="DataGridRow">
                                <Setter Property="Background" Value="{Binding LabelForeColor}"/>
                            </Style>
                        </DataGrid.RowStyle>
                    </DataGrid>
hgqdbh6s

hgqdbh6s1#

C#程式码

private void dgMeanings_Clicked(object sender, SelectedCellsChangedEventArgs e)
{
    // Make sure at least 1 cell was selected
    if (dgMeanings.SelectedCells.Count > 0)
    {

        // Get text from cell by getting the cell content at the selected cell's index
        // Next, convert it to a TextBox and grab the text from the TextBox
        string myText = ((TextBox)dgMeanings.SelectedCells[0].Column.GetCellContent(dgMeanings.SelectedCells[0].Item)).SelectedText;

        // Unselect cells to allow for a re-click
        dgMeanings.UnselectAllCells();
    }
}

XAML格式

SelectedCellsChanged="dgMeanings_Clicked"添加到DataGrid定义中
如果此操作未按预期运行或您有任何疑问,请告诉我

相关问题