Tommehh
Tommehh

Reputation: 946

Bind List<object> to Datagrid WPF, C#

I'm totally new to WPF so heres my code:

 <DataGrid x:Name="dgVarConfig" ItemsSource="{Binding varConfigList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="10,59,0,0" VerticalAlignment="Top" Height="403" Width="1278" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False" HeadersVisibility="Column">

            <DataGrid.Columns>
                <DataGridTextColumn  Width="auto" Header="Match Ausdruck" Binding="{Binding match_expression}"></DataGridTextColumn>
            </DataGrid.Columns>
</DataGrid>

My Files: MainWindow.xaml, MainController.cs, VarConfigDAO.cs

the varConfigDAO.cs returns the list to the MainController, and the MainController.cs returns it do MainWindows.xaml.

This is the VarConfig.cs:

 public class VarConfig
    {
        public int id { get; set; }
        public String group { get; set; }
        public String machine { get; set; }
        public String match_expression { get; set; }
        public String variant_new_1 { get; set; }
        public String calc_formula_1 { get; set; }
        public String variant_new_2 { get; set; }
        public String calc_formula_2 { get; set; }
    }

It works if i set the itemssource programmaticly:

dgVarConfig.Itemssource = mainController.loadVarConfigList();

But thats not what i want because i want to update the list via the grid (insert, delete, update lines => Mode=TwoWay)

Any clue how i can fill the itemssource via xaml?

Upvotes: 2

Views: 4039

Answers (2)

Clemens
Clemens

Reputation: 128013

Create a view model class with a property that holds a collection of VarConfig objects. The collection should notify the view about changes (like added or removed elements). An appropriate collection type would therefore be ObservableCollection:

public class ViewModel
{
    public ObservableCollection<VarConfig> VarConfigList { get; }
        = new ObservableCollection<VarConfig>();
}

Set the DataContext of your UI (e.g. your MainWindow) to an instance of the view model, for example in code behind in the MainWindow constructor like this:

public MainWindow()
{
    InitializeComponent();

    var viewModel = new ViewModel();
    // fill viewModel.VarConfigList

    DataContext = viewModel;
}

Bind to the VarConfigList property in XAML. It is not necessary to set Mode=TwoWay or UpdateSourceTrigger=PropertyChanged, as the ItemsSource property is only bound one-way (the DataGrid - or any other ItemsControl - never sets it):

<DataGrid ItemsSource="{Binding VarConfigList}" ...>
    ...
</DataGrid>

Finally, if you also want the UI to react on changes of the individual VarConfig properties, it should implement the INotifyPropertyChanged interface:

public class VarConfig : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private int id;
    public int Id
    {
        get { return id; }
        set
        {
            id = value;
            OnPropertyChanged(nameof(Id));
        }
    }

    // similar code for the other properties
}

Note the casing. It's widely accepted to write C# property names in PascalCase.

Upvotes: 7

PScr
PScr

Reputation: 459

You are going to have to set the DataContext for your DataGrid to whatever object has varConfigList in it. Then, the DataGrid will be able to see varConfigList and do it's stuff. You don't give a lot of code, so it I can't see what you are after, but I'm going to make some guesses, and see if they help.

There are a few ways to do it. I take it the DataGrid is in your MainWindow, so it will normally inherit its DataContext from there; but you can also set it individually.

Here is one possible way:

MainWindow

<Window ...>
    <DataGrid ... ItemsSource={Binding VarConfigList} ... />
</Window>

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

MainController

class MainControler 
{
    private var _varConfigList;
    public var VarConfgList { get { return _varConfigList; } }
    ...

    public MainControler()
    { 
       // set stuff up 
    }
}

Then your next problem is how to get the DataGrid to treat VarConfigList as you want, but that is another story.

In my limited experience, sorting out the DataContexts has been the biggest obstacle in learning WPF.

Upvotes: 0

Related Questions