【问题标题】:How can I make a messagebox or textbox pop up when a specific cell is double clicked in my DataGrid in C# WPF在 C# WPF 中的 DataGrid 中双击特定单元格时,如何弹出消息框或文本框
【发布时间】:2015-09-26 14:20:13
【问题描述】:

我有一个 DataGrid,我正在使用 C# WPF。我想找到一种方法,以便当用户双击我最后一列中的一个单元格时,会弹出一个文本框并显示该特定单元格中的所有内容。反正有可能这样做吗?你会认为有一种方法可以让我像

if (myDataGridObj.cellselected[i] is in this Column)

{ 
  do stuff
}

我基本上想在用户双击特定单元格时返回该特定单元格的内容。我不想弹出任何东西,除非该特定单元格属于某个列名并被单击。我只想归还那个单元格的内容。

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:
    1. 我们需要处理单元格的 MouseDoubleClickEvent。
    2. 检查是否在我们想要的单元格中双击。
    3. 然后我们需要在网格上显示一个带有单元格数据的 TextBox。
    4. 如果我们在 TextBox 之外的 Grid 上单击任意位置,则应该隐藏 TextBox。

    在本例中,双击地址列的最后一列,显示文本框。

    以下代码可以直接使用:

    <Window x:Class="WpfDataControls.DataGrid._32792409.Win32792409"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Win32792409" Height="329.323" Width="568.421">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="192*"/>
                <RowDefinition Height="107*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="87*"/>
                <ColumnDefinition Width="53*"/>
            </Grid.ColumnDefinitions>
            <DataGrid x:Name="DgrdEmployees"  PreviewMouseDown="Grid_PreviewMouseDown" HorizontalAlignment="Left" Margin="29,30,0,0" VerticalAlignment="Top" Height="148" Width="305" LoadingRow="DgrdEmployees_LoadingRow" />
            <TextBox x:Name="TbDgrdCellContent" Visibility="Hidden" Width="200" Height="100" HorizontalAlignment="Left" Margin="44,59,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Background="#FFFFF3F3" BorderThickness="4"/>
        </Grid>
    </Window>
    
    using System;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Windows.Data;
    using System.Diagnostics;
    
    using System.Windows.Media;
    
    namespace WpfDataControls.DataGrid._32792409
    {
        /// <summary>
        /// Interaction logic for Win32792409.xaml
        /// </summary>
        public partial class Win32792409 : Window
        {
            public Win32792409()
            {
                InitializeComponent();
    
                ICollectionView view = CollectionViewSource.GetDefaultView(DataStore.Employees);
                DgrdEmployees.ItemsSource = view;
            }
    
            private void Grid_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                TbDgrdCellContent.Visibility = System.Windows.Visibility.Hidden;            
            }
    
            private void DgrdEmployees_LoadingRow(object sender, DataGridRowEventArgs e)
            {
                MouseButtonEventHandler handler = new MouseButtonEventHandler(DgrdCell_MouseDoubleClick);
                e.Row.AddHandler(DataGridCell.MouseDoubleClickEvent, handler);
            }
    
            private void DgrdCell_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
            {
                Debug.WriteLine("Double click");
    
                DataGridCellInfo cellInfo = DgrdEmployees.CurrentCell;
                DataGridColumn col = cellInfo.Column;
    
                if (DgrdEmployees.Columns.IndexOf(col) < DgrdEmployees.Columns.Count - 1) return;
    
                FrameworkElement content = col.GetCellContent(cellInfo.Item);
    
                TbDgrdCellContent.Visibility = System.Windows.Visibility.Visible;
                TbDgrdCellContent.Text = ((TextBox)content).Text;
                TbDgrdCellContent.Focus();
            }
    
        }
    
        public class DataStore
        {
            static ObservableCollection<Employee> _employees;
            public static ObservableCollection<Employee> Employees { get { return _employees; } set { _employees = value; } }
    
            static DataStore() {
                _employees = new ObservableCollection<Employee>();
                _employees.Add(new Employee() { Name = "Anjum", Address = "nizamuddin" });
                _employees.Add(new Employee() { Name = "Ashok", Address = "bharat nagar" });
                _employees.Add(new Employee() { Name = "BAshok", Address = "bharat nagar" });
                _employees.Add(new Employee() { Name = "DAshok", Address = "bharat nagar" });
                _employees.Add(new Employee() { Name = "TAshok", Address = "bharat nagar" });
                _employees.Add(new Employee() { Name = "GAshok", Address = "bharat nagar" });
    
            }
    
        }
    
        public class Employee {
    
            public String Name { get; set; }
            public String Address { get; set; }        
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      你能做一个单独的窗口对话框吗?像这样:

         <Window x:Class="MyDialog">
             <StackPanel>
                 <TextBlock Text="" />
                 <TextBox x:Name="MyTextBox" />
                 <Button Content="Done" Click="DoneButton_Click" />
             </StackPanel>
         </Window>
      

      现在你可以有一个简单的代码:

      partial class MyDialog : Window {
      
          public MyDialog(String text) {
              InitializeComponent();
              DialogText = text;
          }
      
          public string DialogText {
              get { return MyTextBox.Text; }
              set { MyTextBox.Text = value; }
          }
      
          private void DoneButton_Click(object sender, System.Windows.RoutedEventArgs e)
          {
              DialogResult = true;
          }
      }
      

      在您的 onDoubleClick(或双击单元格时调用的任何内容)上,您可以创建一个新对话框并设置文本框的文本。

      var dialog = new MyDialog("your content here");
      dialog.ShowDialog();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-19
        • 1970-01-01
        • 2014-04-24
        • 1970-01-01
        • 2022-05-29
        相关资源
        最近更新 更多