简体   繁体   中英

Binding DataGrid and ObservableCollection in C#

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.

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>

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;

I get 6 columns instead of 3, of course they have same content: 3 of those I get from XAML code (and binding in this case works) and 3 are from that line: tabela.ItemsSource = _obsCollection;

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?

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

try the following 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; }

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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