【发布时间】:2018-05-16 15:08:00
【问题描述】:
总的来说,我对 WPF 和 C# 比较陌生。我正在玩它,遇到了一个问题,我觉得这对专家来说是小菜一碟,但我不知道我做错了什么。 我正在尝试创建一个简单的 DataGrid 控件(在 TabControl 内)并将其绑定到 ObservableCollection 对象。 我使用微软在其数据绑定概述中提供的Data Binding Demo 作为我的代码的基础。
主窗口 XAML:
<Window x:Class="PetProject.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:PetProject"
mc:Ignorable="d"
Title="PetProject" Height="350" Width="525">
<Window.Resources>
<CollectionViewSource
Source="{Binding Source={x:Static Application.Current}, Path=Dogs}"
x:Key="DogsDataView" />
</Window.Resources>
<Grid Margin="8,8,8,8">
<TabControl>
<TabItem Header="Dogs">
<DataGrid ItemsSource="{Binding Source={StaticResource DogsDataView}}">
</DataGrid>
</TabItem>
</TabControl>
</Grid>
</Window>
代码隐藏:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace PetProject
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public partial class MainWindow : Window
{
CollectionViewSource DogsDataView;
public MainWindow()
{
InitializeComponent();
DogsDataView = (CollectionViewSource)(this.Resources["DogsDataView"]);
}
}
}
应用 XAML 是
<Application x:Class="PetProject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PetProject"
Startup="AppStartup">
<!--StartupUri="MainWindow.xaml"-->
</Application>
代码隐藏:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace PetProject
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private ObservableCollection<Dog> dogs = new ObservableCollection<Dog>();
void AppStartup(object sender, StartupEventArgs args)
{
LoadData();
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
}
public ObservableCollection<Dog> Dogs
{
get { return this.dogs; }
set { this.dogs = value; }
}
private void LoadData() {
Dog Johnny = new Dog("Johnny",1325);
Dog Diamond = new Dog("Diamond",1327);
this.Dogs.Add(Johnny);
this.Dogs.Add(Diamond);
}
}
}
Dog 只是一个实现 INotifyPropertyChanged 接口的类(它现在什么都不做):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace PetProject
{
public class Dog : INotifyPropertyChanged
{
private string name;
private string number;
public event PropertyChangedEventHandler PropertyChanged;
public Dog(string name, int number)
{
this.name = name;
this.number = number.ToString("D4");
}
protected void NotifyPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
在理解 DataGrid 未填充的原因方面,我将不胜感激。 此外,任何关于不良编码习惯或改进代码的建议都将非常受欢迎,因为我正处于一个非常初始的经验学习阶段。 谢谢!
【问题讨论】:
-
删除 DogsDataView = (CollectionViewSource)(this.Resources["DogsDataView"]);
-
嗨,你能解释一下为什么这是必要的吗?似乎可以在不删除它的情况下工作(在纠正了我遇到的私人/公共问题之后)。我现在意识到它现在什么都不做 - 这可能会在未来发生变化。
-
因为你设置 DogsDataView = DogsDataView ;一旦你在窗口中使用了命名对象,一旦你通过使用资源得到相同的对象
-
我不明白。我将资源添加到 MainWindow xaml。现在我在代码隐藏中声明它,但是如果我希望它引用相同的数据,我不需要通过xaml资源引用吗?
-
您的 res 有一个键/名称,因此在 MainWindow 中使用它就像这样。DogsDataView - 不需要在 MainWindow 中声明变量(可能将 x:Key 更改为 x:Name) - 顺便说一句,在看看你是否做 MVVM !!
标签: c# wpf data-binding