Maciej Los
Обратите внимание, что вам следует избегать использования кода, который хорошо известен в WinForms. Для получения более подробной информации, пожалуйста, смотрите: WPF против WinForms[^]. Графический интерфейс должен быть определен в XAML
. Видеть: Обзор XAML (WPF) | Microsoft Docs[^].
Очень простой пример:
Файл MainWindow.язык XAML
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1" x:Class="WpfApplication1.MainWindow"
Title="My dates..." Height="350" Width="525">
<DataGrid ItemsSource="{Binding MyDates}" AutoGenerateColumns="False" Width="Auto">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Date, StringFormat={}{0:yyyy/MM/dd}}" Header="Date" Width="auto"/>
</DataGrid.Columns>
</DataGrid>
</Window>
Файл MainWindow.язык XAML.в CSusing 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;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
//your binding source ;)
public ObservableCollection<DateTime> MyDates { get; set; }
public MainWindow()
{
InitializeComponent();
//add 7 dates
MyDates = new ObservableCollection<DateTime>(Enumerable.Range(0, 7).Select(x => DateTime.Today.AddDays(x)));
DataContext = this;
}
}
}
Для начала, пожалуйста, прочтите:
Введение в WPF[^]Windows Presentation Foundation[^]Общие Сведения О Связывании Данных[^]