c# - Datagrid Binding in code behind versus DataGrid binding in XAML


Question: 

I am testing WPF and bindings. I have a demo which is working fine with my bindings done in the code-behind.

Here is my XAML code and C# code.

<Window x:Class="WpfApplication1.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:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">

<Grid>

    <DataGrid x:Name="dgrdMaGrid" />
    <Button x:Name="button" Content="Quitter" HorizontalAlignment="Left" Margin="355,268,0,0" VerticalAlignment="Top" Width="155" Height="44" Click="button_Click"/>

</Grid>

c# :

   public partial class MainWindow : Window
{
    public ObservableCollection<Individu> listeIndividus = new ObservableCollection<Individu>();

    public MainWindow()
    {
        InitializeComponent();

        // Load data to display
        listeIndividus = loadDummyData();

        // Build the binding
        dgrdMaGrid.DataContext = this;
        dgrdMaGrid.ItemsSource = listeIndividus;

    }

I am wondering how to change my XAML code to have the same result but by doing the binding inside the XAML.

I know that I should use ItemsSource="{Binding listeIndividus}", but that's not enough...

<Window x:Class="WpfApplication1.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:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">

<Grid>

    <DataGrid x:Name="dgrdMaGrid" ItemsSource="{Binding listeIndividus}"/>
    <Button x:Name="button" Content="Quitter" HorizontalAlignment="Left" Margin="355,268,0,0" VerticalAlignment="Top" Width="155" Height="44" Click="button_Click"/>

</Grid>

Thanks in advance for your tips

Vincent




1 Answer: 

Where you say "// Build the binding", that's not a binding. You're not creating an instance of the Binding class. That's just an assignment, and the difference matters.

OK, lecture over, now to fix your code.

First, you can't bind to a field, only to a property. So listeIndividus needs a getter:

public ObservableCollection<Individu> listeIndividus { get; } = new ObservableCollection<Individu>();

Second, this is a Binding, but you don't have a viewmodel, so it won't work as it is. This binding will look at Window.DataContext for a property named listeIndividus. Since Window.DataContext is null, it won't find it. Window.DataContext would be the viewmodel, if you had one.

<DataGrid x:Name="dgrdMaGrid" ItemsSource="{Binding listeIndividus}"/>

So tell it where to really look -- on the Window itself:

<DataGrid 
    x:Name="dgrdMaGrid" 
    ItemsSource="{Binding listeIndividus, RelativeSource={RelativeSource AncestorType=Window}}"
    />
 

More Articles


WPF Frame Source Refreshing the Loaded Page

I've come across a strange scenario where a frame refuses to refresh its content.I can kinda understand what's happening but the solution is not coming to me.I have a page that has a frame (Frame1) and several buttons. When I click on a button a page is loaded into the frame. This works perfectly in

android - Horizontal and Vertical linear layout inside a scroll view

I am trying to get a combination of Views established. They must constantly have a Button and Edittext box at the top horizontally next to each other and below that a vertical list of Textviews. The vertical list should be enclosed in a ScrollView to allow the user to scroll down through the TextVie

WPF c# window navigation

i got 2 window , i want to navigate from one another but i when i debug it , it shows that nav is null , why is that so? Does it suppose to be null? because i used the same way to navigate in the past and it work and i try it now , it doesn't work. But if i put nav == null , it says nav.Navigate(n


jquery - CSS for toolbar with UI Slider centered between left and right buttons

I'm attempting to create a 100% width toolbar. This toolbar needs to have a variable number of buttons aligned to the left side, as well as a variable number of buttons aligned to the right. That's the easy part.But now I want to put a jQuery UI slider in the center that takes up the full remaining

formatting - Excel 2007 floating format toolbar customisation

Is it possible in Excel 2007 to customise the floating format bar that is shown when you right-click on a cell? To avoid confusion, I don't mean the "Cell" commandbar menu, but the second floating toolbar with formatting buttons.e.g. is it possible to add a Styles dropdown, or have any other text al

Debugging with PhpStorm locally

Kind of a noob question. I'm uploading to a remote server. I've been advised to debug locally (versus remotely). How am I to pull up my files in a browser, then use the debugging tools in PhpStorm? As it stands, my remote host (in PhpStorm) is configured. I've done my own research but can't seem to


xamarin.forms - How to add background image to grid layout in xamarin forms?

I have four rows and two columns.I need to display the background image to full grid.Please provide me solution.I had referred this creating background image in content page link:Xamarin Forms - how to add background image to a content page.But how to set background image to grid layout.

c# - Custom Control, bind to code behind

Having trouble binding to the data in my code behind for my custom control. Obviously I have got to use the Generic.xaml to style my control, and I want to bind to the data in my control UserProfile.cs. This is the code behind:using System;using System.Windows;using System.Windows.Controls;namespace

PhpStorm: automatically rename file on remote host when renaming it locally

Is there any configuration in PhpStorm that automatically renames file on remote host when you rename it locally and reverse?

xamarin.forms - Margin to a Image in Xamarin Forms

I have an image on my Xamarin.Forms. I want to add some margin from Top. Form.XAML:-<ContentPage.Content> <Grid> <Image x:Name="image" /> </Grid></ContentPage.Content>"Form.XAML.cs:- image.Source = ImageSource.FromFile (Height > Width ? "portrait.jpg"