【问题标题】:wpf datagrid not showing data, but show rowswpf datagrid不显示数据,但显示行
【发布时间】:2015-12-14 19:08:14
【问题描述】:

我正在尝试制作一个简单的 WPF 项目,但遇到了一个问题: DataGrid 绑定到 ObservableCollection,它不显示 数据,但它显示了正确的行数。这是它的外观

这是 XAML:

<Window x:Class="008.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:008"
        mc:Ignorable="d"
        Title="MainWindow">

    <StackPanel>
        <StackPanel Orientation="Horizontal" Margin="10">
            <Button Name="AddButton" HorizontalAlignment="Left"  Margin="10">Add an element</Button>
            <Button Name="DeleteOldButton" HorizontalAlignment="Left" Margin="10">Delete old files</Button>
            <Button Name="ShowPop" HorizontalAlignment="Left" Margin="10">Show most popular element</Button>
        </StackPanel>

        <DataGrid Name="dgrid" CanUserResizeColumns="True"
                  CanUserAddRows="False"
                  IsReadOnly="True"
                  DataContext="files"
                  ItemsSource="{Binding}"

                  Width="Auto">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
                <DataGridTextColumn Header="Date created" Binding="{Binding Created}"/>
                <DataGridTextColumn Header="Times Open" Binding="{Binding TimesOpen}"/>

            </DataGrid.Columns>
        </DataGrid>

    </StackPanel>
</Window>

这是代码:

public partial class MainWindow : Window
    {
    ObservableCollection<File> files;

    public MainWindow()
    {

        InitializeComponent();
        files = new ObservableCollection<File>();
        files.Add(new File("r", DateTime.Now));
        files.Add(new File("o", DateTime.Now));
        files.Add(new File("a", DateTime.Now));
        files.Add(new File("d", DateTime.Now));

    }

}

我已经尝试将 Window DataContext 设置为文件,但没有成功

UPD:这是 File 类:

class File
    {
       public string Name { get; set; }
       public DateTime Created { get; set; }
       public int TimesOpen { get; set; }

        public File(string s, DateTime d)
        {
            Created = d;
            Name = s;
            TimesOpen = 0;
        }
        public void Open()
        {
            TimesOpen++;
        }
    }

UPD: 实现了 INotifyPropertyChanged。没有帮助。文件定义如下:

    class File: INotifyPropertyChanged 

    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string name;
        public string Name
        {
            get { return name; }
            set
            {
                if (!value.Equals(name,StringComparison.InvariantCulture)) {
                    OnPropertyChanged("Name");
                    name = value;
                }                     
            }

        }
        private DateTime created;
       public DateTime Created
        {
            get { return created; }
            set
            {
                if (!created.Equals(value))
                {
                    OnPropertyChanged("Created");
                    created = value;
                }
            }
        }
        private int times_open;
       public int TimesOpen
        {
            get { return times_open; }
            set
            {
                if (times_open != value)
                {
                    OnPropertyChanged("TimesOpen");
                    times_open = value;
                }
            }
        }

        public File(string s, DateTime d)
        {
            Created = d;
            Name = s;
            TimesOpen = 0;
        }

        void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }

        public void Open()
        {
            TimesOpen++;
        }
    }
}

【问题讨论】:

  • 你能告诉我们文件类型的定义吗?
  • 是的,当然,请参阅 UPD
  • 你试过让File实现INotifyPropertyChanged吗?将它放在 Name、Created 和 TimeOpen 属性上。
  • 查看您的异常情况——我看到以下内容:System.Windows.Data Error: 40 : BindingExpression path error: 'TimesOpen' property not found on 'object' ''Char' (HashCode=7536755)'. BindingExpression:Path=TimesOpen; DataItem='Char' (HashCode=7536755); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

标签: c# wpf xaml datagrid


【解决方案1】:

你做错了几件事 - 您应该为窗口设置数据上下文
- 您的项目来源绑定到文件。但是等等..
- 文件必须是数据上下文中的一个属性,即主窗口
- 另外你应该考虑在你的模型中实现 INotifyPropertyChanged

<DataGrid Name="dgrid" CanUserResizeColumns="True"
        CanUserAddRows="False"
        IsReadOnly="True"
        ItemsSource="{Binding Files}"
        Width="Auto">

    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
        <DataGridTextColumn Header="Date created" Binding="{Binding Created}"/>
        <DataGridTextColumn Header="Times Open" Binding="{Binding TimesOpen}"/>
    </DataGrid.Columns>
</DataGrid>

在你的代码后面使用

public partial class MainWindow : Window
{
    public ObservableCollection<File> Files {get; set;}


    public MainWindow()
    {

        InitializeComponent();
        DataContext = this;
        Files = new ObservableCollection<File>();
        Files.Add(new File("r", DateTime.Now));
        Files.Add(new File("o", DateTime.Now));
        Files.Add(new File("a", DateTime.Now));
        Files.Add(new File("d", DateTime.Now));
        Files.Add(new File("d", DateTime.Now));
        Files.Add(new File("d", DateTime.Now));
    }
}

【讨论】:

  • 实现了 INotifyPropertyChanged,尝试将 DataContext 设置为 this 和 files,将 files 设为 public property-nothing
  • @small-j 不,您不应该将数据上下文设置为文件。如上所示,在 mainwindow 的构造函数中将 datacontext 设置为 this。然后将 ItemsSource 设置为 Files。你可以复制上面的代码。您甚至不需要实现 INotifyPropertyChanged 即可查看网格中的数据。当您想稍后通知 UI 数据更改时,INotifyPropertyChanged 将很有用。
  • 实现网格数据的类必须实现 INotifyPropertyChanged。
【解决方案2】:

试试这个..MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {

        InitializeComponent();
        Files = new ObservableCollection<File>();
        Files.Add(new File("r", DateTime.Now));
        Files.Add(new File("o", DateTime.Now));
        Files.Add(new File("a", DateTime.Now));
        Files.Add(new File("d", DateTime.Now));
        DataContext = this;
    }
}

public ObservableCollection<File> Files { get; set; }

MainWindow.xaml (sn-p):

<DataGrid Name="dgrid" CanUserResizeColumns="True"
                  CanUserAddRows="False"
                  IsReadOnly="True"
                  ItemsSource="{Binding Files}"
                  Width="Auto">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
        <DataGridTextColumn Header="Date created" Binding="{Binding Created}"/>
        <DataGridTextColumn Header="Times Open" Binding="{Binding TimesOpen}"/>
    </DataGrid.Columns>
</DataGrid>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    相关资源
    最近更新 更多