Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi everybody.
I copy/paste this example code from DataGrid.ItemsSource Property (System.Windows.Controls) | Microsoft Learn[^]
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>

So as you know, to set the ItemsSource to autogenerate the columns, it uses below code in C# code.
C#
dataGrid1.ItemsSource = Customer.GetSampleCustomerList();

So my question is that is it possible to do this in XAML? I mean how to bind in XAML WPF, not C# ?

What I have tried:

I tried below code but no luck. :-(
XML
ItemsSource="{Binding Customer.GetSampleCustomerList}"
Posted
Updated 31-Dec-23 5:27am
v5
Comments
Dave Kreskowiak 31-Dec-23 18:10pm    
XAML cannot call a method, but it can bind to properties in a class. Properties in a class can call methods.
Sh.H. 1-Jan-24 0:37am    
Thanks. What do you mean by "call method" ?
Where I needed to call a method?
I think you did not understand my question!
Dave Kreskowiak 1-Jan-24 12:35pm    
GetSampleCustomerList is a "method". Methods are also referred to as "functions" in other languages.

In C#, if you have to put parenthesis on the end of the name to call it, that's a method. If you don't, that's a Property.

Oh, and I very much understand the question.
[no name] 1-Jan-24 17:25pm    
What is so hard to understand? You assign the "collection" (from "wherever") to a "property"; then BIND TO THE PROPERTY. That is BINDING IN XAML. Your "method" for retrieving the collection in the first place is "C# code" after all. "Listen" to what the others are saying.

My WPF is a bit rusty, but I would suggest that you set the window's DataContext to a view model instance and set the DataGrid Items source to a CustomerList property defined in the view model. So the Xaml would look like this:

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>



The view model could be simply defined as:

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


I would include the Customer class as in the example but update the GetSampleCustomerList method so that it uses C#12 syntax.

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)
     ];
 }

 
Share this answer
 
Comments
Sh.H. 1-Jan-24 0:35am    
Thanks. But you changed the original code totally!
Beside, you still bind it in C# !!! And it is not bind in WPF !!!
I questioned that how can I bind in XAML, not C#!
George Swan 1-Jan-24 1:42am    
My understanding is that the binding is set in the xaml with <DataGrid x:Name="DataGrid" ItemsSource="{Binding CustomerList}" AutoGenerateColumns="True" />. I updated the method as I believe that it is best practice to not to learn legacy code when better alternatives are available. I am also hesitant about posting other people's code under my name. However, the original version will work fine. As I mentioned before, my WPF skills are a bit rusty but there are plenty of very smart posters on here who should be able to help you further. Best wishes, George.
Dave Kreskowiak 1-Jan-24 12:37pm    
You're not understanding this at all. YOU CANNOT CALL A METHOD FROM XAML. YOU CAN ONLY BIND TO PROPERTIES! So you have no choice but to change both the XAML AND C# to support the binding from XAML.
Maciej Los 6-Jan-24 3:14am    
5ed!
There is no in-built way that you can directly bind your ItemsSource to the result of a method. Anything you do is going to involve C# at some point. Theoretically, it might be possible to accomplish something of what you want by writing a custom Blend Behavior, but that is still going to involve C#.
 
Share this answer
 
Comments
Maciej Los 6-Jan-24 3:14am    
5ed!
Yes, it is possible, but not recommended. Here are some examples: Bind to a method in WPF? - Stack Overflow[^]
 
Share this answer
 
Comments
Sh.H. 5-Jan-24 7:50am    
@Graeme_Grant
Thanks.
I read the URL you sent. None of the worked related to my question.
Also please let me know why you not recommend?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900