结合DataGrid和的ObservableCollection在C# [英] Binding DataGrid and ObservableCollection in C#

查看:317
本文介绍了结合DataGrid和的ObservableCollection在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有得到一些3列出里面的字符串的函数。我用的ObservableCollection保持它内部的3名单。

I'm having a function that gets some 3 lists with strings inside. I've used ObservableCollection for keeping those 3 lists inside of it.

这是的ObservableCollection的定义:

This is a definition of ObservableCollection:

public partial class CreateAreaDialogWindow : System.Windows.Window
    {
        ObservableCollection<Tuple<string, string, string>> _obsCollection = new ObservableCollection<Tuple<string, string, string>>();

    }

使用方法添加列表后,我做了我正在写一些数据网格的结果。数据网格的定义是这样的:

After adding lists with method I've made I'm writing results in some DataGrid. Definition of DataGrid is like this:

<DataGrid Grid.Column="0" AutoGenerateColumns="True" Height="206" HorizontalAlignment="Left" Margin="12,265,0,0" Name="tabela" VerticalAlignment="Top" Width="556" SelectionChanged="tabela_SelectionChanged" Grid.RowSpan="2" ItemsSource="Binding _obsCollection">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Type" Binding="{Binding Item1}"/>
                <DataGridTextColumn Header="MapTo" Binding="{Binding Item2}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding Item3}"/>
                <DataGridCheckBoxColumn Header="Controller"/>
                <DataGridCheckBoxColumn Header="Service"/>
                <DataGridCheckBoxColumn Header="Injection"/>
            </DataGrid.Columns>
        </DataGrid>

除了在3个不同的列串3列出我有3个柱复选框。这是什么问题是,我不知道如何做一个绑定以适当的方式。
例如,如果我把这个线在我的code:

Beside 3 lists of strings in 3 different columns I'm having 3 more columns with checkboxes. What is the problem is that I don't know how to do a binding in a proper way. For example if I put this line in my code:

tabela.ItemsSource = _obsCollection;

我得到6列,而不是3,当然他们有相同的内容:那些3我从XAML code得到(在这种情况下,结合工作)和3是从该行: tabela.ItemsSource = _obsCollection;

当我删除: tabela.ItemsSource = _obsCollection; 我得到复选框一个空表。
所以我知道这个问题是在恶劣的约束力,但有人有一个想法如何编辑这个code,所以我可以删除 tabela.ItemsSource = _obsCollection; 和仍然得到3列使用列表内容和列标题:?类型,MapTo和名称

When I delete: tabela.ItemsSource = _obsCollection; I get an empty table with checkboxes. So I know that the problem is in bad binding, but does someone has an idea how to edit this code so I can remove: tabela.ItemsSource = _obsCollection; and still get 3 columns with lists content and column headers: Type, MapTo and Name?

推荐答案

所有你有什么是设置的AutoGenerateColumns =FALSE并创建类似 MyViewModel 这将作为您的DataContext

All what you have to is to set the AutoGenerateColumns="False" and create something like MyViewModel which will act as your datacontext

请尝试以下code:

 public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MyViewModel();  
        }
    }
    public class MyViewModel

    {
          public MyViewModel()
          {
              for (int i = 0; i < 1; i++)
              {
                  _obsCollection.Add(new Tuple<string, string, string>("Test" + i, "Test2" + i, "Test3" + i));   
              }
          }
        ObservableCollection<Tuple<string, string, string>> _obsCollection = new ObservableCollection<Tuple<string, string, string>>();

        public ObservableCollection<Tuple<string, string, string>> MyObsCollection
        {
            get { return _obsCollection; }

        }
    }

这篇关于结合DataGrid和的ObservableCollection在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆