Est-il possible de mettre ce code en XAML WPF ?

la programmation


Salut tout le monde.
Je copie/colle cet exemple de code de Propriété DataGrid.ItemsSource (System.Windows.Controls) | Microsoft Apprendre[^]

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>

Donc, comme vous le savez, pour configurer ItemsSource pour qu’il génère automatiquement les colonnes, il utilise le code ci-dessous en code C#.

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

Ma question est donc la suivante : est-il possible de faire cela en XAML ? Je veux dire, comment lier en XAML WPF, pas en C# ?

Ce que j’ai essayé :

J’ai essayé le code ci-dessous mais pas de chance. 🙁

XML
ItemsSource="{Binding Customer.GetSampleCustomerList}"

Solution 1

Mon WPF est un peu rouillé, mais je vous suggère de définir la fenêtre DataContext à une instance de modèle de vue et définissez le DataGrid Les éléments proviennent d’un CustomerList propriété définie dans le modèle de vue. Le Xaml ressemblerait donc à ceci :

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>

Le modèle de vue pourrait être simplement défini comme :

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

J’inclurais le Customer classe comme dans l’exemple mais mettez à jour la GetSampleCustomerList méthode afin qu’elle utilise la syntaxe 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をコピーしました