簡體   English   中英

wpf datagrid不顯示數據,但顯示行

[英]wpf datagrid not showing data, but show rows

我正在嘗試制作一個簡單的WPF項目,但有一個問題:DataGrid綁定到ObservableCollection,它不顯示數據,但顯示正確的行數。

WPF DataGrid顯示正確的行數,但不顯示數據

這是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++;
        }
    }
}

您做錯了幾件事-您應該為窗口設置數據上下文
-您的商品來源已綁定到文件。 可是等等..
-文件必須是數據上下文中的屬性,即主窗口
-另外,您應該考慮在模型中實現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));
    }
}

試試這個.. 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 (摘要):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM