在WPF绑定上缺少一些东西

时间:2016-03-16 04:25:09

标签: c# wpf xaml

刚开始WPF&无法在DataGrid上获得简单的WPF绑定工作,我不知道如何调试。绑定类初始化程序执行但DataGrid上没有显示任何内容。最小代码背后我保留了XAML&绑定对象尽可能简单。谢谢你的帮助。

<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;  }
    }
}

1 个答案:

答案 0 :(得分:1)

绑定 仅适用于属性而非成员变量。

只需将课程更改为

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; }
}