简体   繁体   English

wpf在数据网格中的可编辑单元格上动态调出上下文菜单

[英]wpf bring up a context menu dynamically on an editable cell in a datagrid

The scenario is: 该方案是:

I have a datagrid that uses a DataTable as an ItemsSource. 我有一个使用DataTable作为ItemsSource的数据网格。

dataGrid.ItemsSource = LogFileItemDataTable.DefaultView;

I can inline edit fields in there, and one is a textbox. 我可以在其中内联编辑字段,其中一个是文本框。

If it is a text box, I'd like to be able to select some text and dynamically bring up a context menu with item "Contains..." and "Does not contain..." and then do some stuff. 如果它是一个文本框,我希望能够选择一些文本并动态显示带有“包含...”和“不包含...”项的上下文菜单,然后执行一些操作。

But I was having trouble trying to get an event and setting a context menu to the textbox that is currently being edited. 但是我在尝试获取事件并将上下文菜单设置到当前正在编辑的文本框中时遇到了麻烦。

My datagrid does not explicitly setup columns, it just uses ItemsSource with a datatable. 我的datagrid没有显式设置列,它仅将ItemsSource与数据表一起使用。

  <DataGrid x:Name="dataGrid" Margin="10,35,10,40">
  </DataGrid>

if you are working on a datagrid template column, in the template add the following: 如果您正在处理datagrid模板列,请在模板中添加以下内容:

<TextBox
  Name="cxmTextBox" 
  Grid.Row="1"
  AcceptsReturn="True"
  AcceptsTab="True"
  VerticalScrollBarVisibility="Visible"
  TextWrapping="Wrap"
>
  <TextBox.ContextMenu>
    <ContextMenu 
      Name="cxm"
      Opened="CxmOpened"
    >
      <MenuItem 
        Header="Contains"
        Name="cxmItemContains" 
        Click="ClickContains" 
      />
      <MenuItem 
        Header="Does not contain" 
        Name="cxmItemNotContain"
        Click="ClickNotContain" 
      />
    </ContextMenu>
  </TextBox.ContextMenu>
</TextBox>

and add the code behind: 并在后面添加代码:

public void CxmOpened(Object sender, RoutedEventArgs args)
{
    // Only allow menu item if something is selected
    if (cxmTextBox.SelectedText == "")
        cxmItemContains.IsEnabled = cxmItemNotContains.IsEnabled = false;
    else
        cxmItemContains.IsEnabled = cxmItemNotContains.IsEnabled = true;

    // Only allow paste if there is text on the clipboard to paste.
    if (Clipboard.ContainsText())
        cxmItemPaste.IsEnabled = true;
    else
        cxmItemPaste.IsEnabled = false;
}

public void cxmItemContains(object sender, RoutedEventArgs e) { //do your stuff}

public void cxmItemNotContains(object sender, RoutedEventArgs e) { //do your stuff}

Use a RichTextBox for your DataGrid Cell s and then bring up ContextMenu from code by using SelectionChanged Event . 为您的DataGrid Cell使用RichTextBox ,然后使用SelectionChanged Event从代码中调出ContextMenu Something like this: 像这样:

<DataGridTemplateColumn Header="First" IsReadOnly="False" Width="*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <RichTextBox x:Name="rtbFirst" SelectionChanged="rtbFirst_SelectionChanged">
                                <FlowDocument IsOptimalParagraphEnabled="True" IsHyphenationEnabled="True">
                                    <Paragraph>
                                       Some Text
                                    </Paragraph>
                                </FlowDocument>
                            </RichTextBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

Then 然后

private void rtbFirst_SelectionChanged(object sender, RoutedEventArgs e)
        {
            //do your context menu stuff here
        }

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

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