【问题标题】:Missing something on WPF Binding在 WPF 绑定上缺少一些东西
【发布时间】:2016-03-16 04:25:09
【问题描述】:

刚启动 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;  }
    }
}

【问题讨论】:

    标签: c# wpf xaml


    【解决方案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; }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      • 2013-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多