【问题标题】:Read each cell data of a custom Datagrid - WPF C#读取自定义 Datagrid 的每个单元格数据 - WPF C#
【发布时间】:2018-06-01 15:00:04
【问题描述】:

我定义了一个自定义数据网格,其列由文本框、组合框和按钮组成。我还使用 XAML 绑定填充了每个组合框上的数据。现在,我想从数据网格中读取每个选定的组合框值和文本框值。但我找不到任何阅读方式。我在论坛上进行了研究,并找到了与 DataGridView 相关的解决方案,但对于 DataGrid 来说并不多。这是我定义自定义数据网格以及如何在其中填充数据的示例代码。

public class Data
{

    public List<string> Disciplines { get; set; }
    public List<string> Members { get; set; }
    public List<string> ActionType { get; set; }


    public Data()
    {
        this.Disciplines = new List<string>
        {
            "Architecture", "Mechanical", "Structure"
        };

        this.Members = new List<string>
        {
            "Ali", "Mubashar", "Muffassir", "Nitin"
        };

        this.ActionType = new List<string>
        {
            "Take Action",
            "For Information"
        };




    }
}

public MainWindow()
{
    InitializeComponent();
    datagrid_additionalinfo.Items.Add(new Data());


}

<DataGrid  Name="datagrid_additionalinfo" Margin="20,0,20,0" 
                   IsReadOnly="False" SelectionMode="Single" CanUserAddRows="True" 
                   AutoGenerateColumns="False" SelectionUnit="Cell" >


            <DataGrid.Columns>

                <DataGridTemplateColumn Header="Comment"  Width="*" MinWidth="130">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Width="235"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="Discipline"   Width="100" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox Width="200" ItemsSource="{Binding Disciplines}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="Members"  Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox Width="200" ItemsSource="{Binding Members}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="Action Type"  Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox Width="200" ItemsSource="{Binding ActionType}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

            </DataGrid.Columns>


  </DataGrid>

自定义数据网格如下所示: 将不胜感激任何帮助。谢谢

【问题讨论】:

  • 只需将“SelectedItem”属性作为绑定添加到您的代码中。
  • 嗨 Urgur,我试过了,但我在选定的项目属性上得到了“空”值。任何解决方法将不胜感激。
  • 我已经使用 ViewModel 附加了干净的代码。

标签: c# wpf xaml datagridview datagrid


【解决方案1】:

除了集合属性public List&lt;string&gt; Disciplines { get; set; },还为所选项目声明一个属性

public string SelectedDiscipline { get; set; }

并将其绑定到 ComboBox

<ComboBox Width="200" 
          SelectedIndex="0"
          SelectedItem="{Binding SelectedDiscipline, UpdateSourceTrigger=PropertyChanged}"
          ItemsSource="{Binding Disciplines}"/>

并对其他属性执行相同操作

这样您就可以访问所有值,而无需直接使用 DataGridRow 单元格

【讨论】:

  • 嗨,Ash,感谢您分享这个可能性。我尝试了你的解决方案,我不确定为什么我在“selectedDiscipline”上得到“null”值。有什么解决方法吗?
  • 字符串属性的初始值为null。可以使用视图模型中的集合中的一些值对其进行初始化。添加SelectedIndex="0" 选择第一项。还可以尝试添加UpdateSourceTrigger=PropertyChanged 以确保属性更新立即发生
  • 嗨,Ash,我试过 SelectedIndex="0" 但我仍然得到空值。另外,我们需要在哪里更改 UpdateSourceTrigger 的属性,因为我在任何地方都没有找到它的名称..
  • 感谢 Ash,它成功了。我错过了 UpdateSourceTrigger 属性。非常感谢。你节省了我的工作日;)
【解决方案2】:

我已经看到这里存在基本的理解问题。您将代码与 MVVM 结构混合在一起。而不是那个代码;

datagrid_additionalinfo.Items.Add(new Data());

尝试使用 ObservableCollection&lt;Data&gt; 作为 ViewModel 中 DataGrid 的 ItemSource。使用您的 ViewModel 作为您的视图的 DataContext。之后,您可以绑定 SelectedItems。

这里是代码;

MainWindow.xaml

<Window x:Class="DataGridComboBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DataGridComboBox"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

        <DataGrid  Name="datagrid_additionalinfo" Margin="20,0,20,0" 
                   ItemsSource="{Binding DataItems}" 
                   SelectedItem="{Binding SelectedDataRow, UpdateSourceTrigger=PropertyChanged}"
                   IsReadOnly="False" SelectionMode="Single" CanUserAddRows="True" 
                   AutoGenerateColumns="False"  >


            <DataGrid.Columns>

                <DataGridTemplateColumn Header="Comment"  Width="*" MinWidth="130">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Width="235"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="Discipline"   Width="100" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox Width="200" ItemsSource="{Binding Disciplines}" 
                                      SelectedItem="{Binding DataContext.SelectedDisipline, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="Members"  Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox Width="200" ItemsSource="{Binding Members}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="Action Type"  Width="100">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox Width="200" ItemsSource="{Binding ActionType}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

            </DataGrid.Columns>


        </DataGrid>
    </Grid>
</Window>

MainWindow.xaml.cs

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainWindowViewModel(); 
        }
    }


**MainWindowViewModel**

using System.Collections.ObjectModel;
using System.ComponentModel;

...

 public class MainWindowViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private ObservableCollection<Data> m_DataItems;

        public ObservableCollection<Data> DataItems
        {
            get { return m_DataItems; }
            set { m_DataItems = value; }
        }


        private Data m_SelectedDataRow;
        public Data SelectedDataRow
        {
            get { return m_SelectedDataRow; }
            set { m_SelectedDataRow = value; }
        }


        private string m_SelectedDisipline;
        public string SelectedDisipline
        {
            get { return m_SelectedDisipline; }
            set { m_SelectedDisipline = value; }
        }

        public MainWindowViewModel()
        {
            m_DataItems = new ObservableCollection<Data>();

            //Fill Items 
            Data data; 
            for (int i = 0; i < 10; i++)
            {
                data = new Data();
                m_DataItems.Add(data);
            }
        }

        private void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }

【讨论】:

  • 嗨,Ugur,仍然无法从单元格中获取值。正如你所说,我编辑了代码。还有其他解决方法吗?
  • 感谢 Ugur 分享解决方案。非常感谢您的支持。但是,Ash 提供的上述解决方案也有效。今晚晚些时候我会挖掘你的代码。谢谢
  • 不客气。但要小心,快速肮脏的解决方案可以节省时间,但最终可能需要使用完整 MVVM 模式的更好解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-21
  • 2013-06-08
  • 1970-01-01
  • 2011-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多