Home > Enterprise >  Simple GridView not xaml binding
Simple GridView not xaml binding

Time:09-21

I'm just trying to get minimalistic gridview to work using binding in the xaml. If I assign the ItemSource in the code-behind it works fine. What am I missing? Just empty grids when I try to do the xaml binding.

xaml:

<StackPanel>
    <telerik:RadGridView x:Name="GridView1" ItemsSource="{Binding MyProperties1}" AutoGenerateColumns="True" />
    <telerik:RadGridView x:Name="GridView2" ItemsSource="{Binding MyProperties2}" AutoGenerateColumns="True" />
</StackPanel>

Code behind:

public partial class MainWindow : Window
{
    public List<MyGridView1Data> MyProperties1 { get; set; }
    public List<MyGridView2Data> MyProperties2 { get; set; }
    public MainWindow()
    {
        InitializeComponent();

        MyProperties1 = new List<MyGridView1Data>();
        MyProperties1.Add(new MyGridView1Data());

        MyProperties2 = new List<MyGridView2Data>();
        MyProperties2.Add(new MyGridView2Data());

        //GridView1.ItemsSource = MyProperties1;
        //GridView2.ItemsSource = MyProperties2;
    }
}

public class MyGridView1Data
{
    public string MyProperty1 { get; set; } = "12345678901234567892123456789312345678941234567895123456789";
    public string MyProperty2 { get; set; } = "12345678901234567892123456789312345678941234567895123456789";
    public string MyProperty3 { get; set; } = "12345678901234567892123456789312345678941234567895123456789";
    public string MyProperty4 { get; set; } = "12345678901234567892123456789312345678941234567895123456789";
    public string MyProperty5 { get; set; } = "12345678901234567892123456789312345678941234567895123456789";
    public string MyProperty6 { get; set; } = "12345678901234567892123456789312345678941234567895123456789";
    public string MyProperty7 { get; set; } = "12345678901234567892123456789312345678941234567895123456789";
    public string MyProperty8 { get; set; } = "12345678901234567892123456789312345678941234567895123456789";
}

public class MyGridView2Data
{
    public string MyProperty1 { get; set; } = "12345678901234567892123456789312345678941234567895123456789";
    public string MyProperty2 { get; set; } = "12345678901234567892123456789312345678941234567895123456789";
    public string MyProperty3 { get; set; } = "12345678901234567892123456789312345678941234567895123456789";
    public string MyProperty4 { get; set; } = "12345678901234567892123456789312345678941234567895123456789";
    public string MyProperty5 { get; set; } = "12345678901234567892123456789312345678941234567895123456789";
    public string MyProperty6 { get; set; } = "12345678901234567892123456789312345678941234567895123456789";
    public string MyProperty7 { get; set; } = "12345678901234567892123456789312345678941234567895123456789";
    public string MyProperty8 { get; set; } = "12345678901234567892123456789312345678941234567895123456789";
}

CodePudding user response:

If I assign the ItemSource in the code-behind it works fine. What am I missing? Just empty grids when I try to do the xaml binding.

The actual issue is because you haven't set the DataContext of the view, in your specific case MainWindow. In order for the MainWindow element to participate in data binding, you have to set the DataContext property. Since the DataContext isn't set, binding isn't happening and why you are not seeing your changes applied.

You can fix your issue a few ways by either:

  • Assigning the DataContext in code behind
  • Adding it to the Xaml
Assigning DataContext in code behind
public MainWindow()
{
   InitializeComponent();
   DataContext = this; //Assign the datacontext to this instance       
}
Adding to Xaml
<Window x:Class="MyApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyApplication"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
   <local:MainWindow/>
</Window.DataContext>
  • Related