Có thể đặt mã này vào XAML WPF không?

lập trình


Chào mọi người.
Tôi sao chép/dán mã ví dụ này từ Thuộc tính DataGrid.ItemsSource (System.Windows.Controls) | Microsoft Tìm hiểu[^]

XML
<Window x:Class="WpfApplication1.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"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"
        x:Name="TestWindow"
        Loaded="WindowLoaded">
    <Grid DataContext="{Binding ElementName=TestWindow, Path=.}">
        <DataGrid x:Name="DataGrid" ItemsSource="{Binding Collection}" />
    </Grid>
</Window>

Vì vậy, như bạn đã biết, để đặt ItemsSource tự động tạo các cột, nó sử dụng mã bên dưới trong mã C#.

C#
dataGrid1.ItemsSource = Customer.GetSampleCustomerList();

Vì vậy, câu hỏi của tôi là có thể thực hiện điều này trong XAML không? Ý tôi là làm thế nào để liên kết trong XAML WPF chứ không phải C#?

Những gì tôi đã thử:

Tôi đã thử mã bên dưới nhưng không gặp may. 🙁

XML
ItemsSource="{Binding Customer.GetSampleCustomerList}"

Giải pháp 1

WPF của tôi hơi cũ, nhưng tôi khuyên bạn nên đặt cửa sổ DataContext vào một phiên bản mô hình xem và đặt DataGrid Nguồn mục tới một CustomerList thuộc tính được xác định trong mô hình khung nhìn. Vì vậy, Xaml sẽ trông như thế này:

C#
<Window x:Class="WpfApplication1.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:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:TestViewModel />
    </Window.DataContext>

    <Grid>
        <DataGrid x:Name="DataGrid" ItemsSource="{Binding CustomerList}" AutoGenerateColumns="True" />
    </Grid>
</Window>

Mô hình khung nhìn có thể được định nghĩa đơn giản là:

C#
public class TestViewModel
 {
     public List<Customer> CustomerList { get; set; }
     public TestViewModel()
     {
         CustomerList = Customer.GetSampleCustomerList();
     }
 }

Tôi sẽ bao gồm Customer lớp như trong ví dụ nhưng cập nhật GetSampleCustomerList phương thức để nó sử dụng cú pháp C#12.

C#
public static List<Customer> GetSampleCustomerList()
 {
         return [
         new("A.", "Zero",
             "12 North Third Street, Apartment 45",
             false, true),
         new("B.", "One",
             "34 West Fifth Street, Apartment 67",
             false, false),
         new("C.", "Two",
             "56 East Seventh Street, Apartment 89",
             true, null),
         new("D.", "Three",
             "78 South Ninth Street, Apartment 10",
             true, true)
     ];
 }

コメント

タイトルとURLをコピーしました