क्या इस कोड को XAML WPF में डालना संभव है?


सबको नमस्ते।
मैं इस उदाहरण कोड को कॉपी/पेस्ट करता हूं DataGrid.ItemsSource संपत्ति (System.Windows.Controls) | माइक्रोसॉफ्ट लर्न[^]

एक्सएमएल
<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>

तो जैसा कि आप जानते हैं, कॉलम को स्वचालित रूप से जेनरेट करने के लिए आइटमसोर्स सेट करने के लिए, यह C# कोड में नीचे दिए गए कोड का उपयोग करता है।

सी#
dataGrid1.ItemsSource = Customer.GetSampleCustomerList();

तो मेरा प्रश्न यह है कि क्या XAML में ऐसा करना संभव है? मेरा मतलब है कि XAML WPF में कैसे बाइंड करें, C# में नहीं?

मैंने क्या प्रयास किया है:

मैंने नीचे दिए गए कोड को आज़माया लेकिन कोई सफलता नहीं मिली। 🙁

एक्सएमएल
ItemsSource="{Binding Customer.GetSampleCustomerList}"

समाधान 1

मेरा डब्ल्यूपीएफ थोड़ा खराब हो गया है, लेकिन मेरा सुझाव है कि आप विंडो सेट करें DataContext मॉडल उदाहरण देखें और सेट करें DataGrid आइटम स्रोत ए CustomerList दृश्य मॉडल में परिभाषित संपत्ति। तो Xaml इस तरह दिखेगा:

सी#
<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>

दृश्य मॉडल को बस इस प्रकार परिभाषित किया जा सकता है:

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

मैं शामिल करूंगा Customer उदाहरण के अनुसार कक्षा लेकिन अद्यतन करें GetSampleCustomerList विधि ताकि यह C#12 सिंटैक्स का उपयोग करे।

सी#
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をコピーしました