Jackobee
Jackobee

Reputation: 11

Missing something on WPF Binding

Just starting WPF & cannot get simple WPF Binding on DataGrid to work and I do not know how to debug. The bound class initializer executes but nothing shows on DataGrid. Minimum code behind and I kept the XAML & bound objects as simple as possible. Thank you for any help.

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfBinding"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  enter code heremc:Ignorable="d" x:Class="WpfBinding.MainWindow"
        xmlns:syncfusion="http://schemas.syncfusion.com/wpf"    
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Window.Resources>
        <local:Simple x:Key="keySimple"/>
    </Window.Resources>
    <Grid>
        <DataGrid x:Name="dg" AutoGenerateColumns="True"
            DataContext="{Binding Source={StaticResource keySimple}}"
            ItemsSource="{Binding Path=Numbers}">
        </DataGrid>
    </Grid>
</Window>


namespace WpfBinding
{
    public class Simple
    {
        public List<Number> Numbers = new List<Number>();

        public Simple()
        {
            Numbers.Add(new Number(5));
            Numbers.Add(new Number(6));
        }
    }

    public class Number
    {
        private int nmb;
        public Number(int x)  {  nmb = x;  }
    }
}

Upvotes: 0

Views: 162

Answers (1)

Sandesh
Sandesh

Reputation: 3004

Bindings works only on properties and not member variables.

Just change your class to

public class Simple
{
    public List<Number> _numbers = new List<Number>();

    public List<Number> Numbers { get { return _numbers; } }

    public Simple()
    {
        _numbers.Add(new Number(5));
        _numbers.Add(new Number(6));
    }
}

public class Number
{
    public int NMB { get; set; }

    public Number(int x) { NMB = x; }
}

Upvotes: 1

Related Questions