Apakah mungkin untuk memasukkan kode ini ke dalam XAML WPF?

pemrograman


Hai semuanya.
Saya menyalin/menempelkan kode contoh ini dari Properti DataGrid.ItemsSource (Sistem.Windows.Kontrol) | Microsoft Belajar[^]

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>

Jadi seperti yang Anda ketahui, untuk menyetel ItemsSource agar membuat kolom secara otomatis, ia menggunakan kode di bawah ini dalam kode C#.

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

Jadi pertanyaan saya adalah apakah mungkin melakukan ini di XAML? Maksud saya bagaimana cara mengikat di XAML WPF, bukan C#?

Apa yang saya coba:

Saya mencoba kode di bawah ini tetapi tidak berhasil. 🙁

XML
ItemsSource="{Binding Customer.GetSampleCustomerList}"

Solusi 1

WPF saya agak berkarat, tapi saya sarankan Anda mengatur jendelanya DataContext ke contoh model tampilan dan atur DataGrid Sumber item ke a CustomerList properti yang ditentukan dalam model tampilan. Jadi Xamlnya akan terlihat seperti ini:

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>

Model tampilan secara sederhana dapat didefinisikan sebagai:

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

Saya akan memasukkan Customer kelas seperti pada contoh tetapi perbarui GetSampleCustomerList metode sehingga menggunakan sintaks 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をコピーしました