Any way to specify binding source datatype?

杨岑 166 Reputation points
2024-04-29T12:16:51.65+00:00

I have Microsoft.Toolkit.Uwp.UI.Controls.DataGrid package in my project. Given the following XAML:

<toolkit:DataGrid x:Name="resultDataGrid"
<!-- How specify datatype of ItemsSource without specifying ItemsSource? -->
                  >
    <toolkit:DataGrid.RowGroupHeaderStyles>
        <Style TargetType="toolkit:DataGridRowGroupHeader">
            <Setter Property="Background" Value="AliceBlue"/>
        </Style>
    </toolkit:DataGrid.RowGroupHeaderStyles>
    <toolkit:DataGrid.Columns>
        <toolkit:DataGridCheckBoxColumn
            Header="X"
            Width="40"
            Binding="{Binding Marked}"/>

        <toolkit:DataGridTextColumn
            Header="Name"
            IsReadOnly="True"
            Binding="{Binding Name}"/>

        <toolkit:DataGridTextColumn
            Header="Path"
            Width="300"
            IsReadOnly="True"
            Binding="{Binding Directory}"/>

        <toolkit:DataGridTextColumn
            Header="Created"
            Width="100"
            IsReadOnly="True"
            Binding="{Binding Created}"/>

        <toolkit:DataGridTextColumn
            Header="Modified"
            Width="100"
            IsReadOnly="True"
            Binding="{Binding Modified}"/>
    </toolkit:DataGrid.Columns>
</toolkit:DataGrid>         

I don't want to use predefined ItemsSource property; I want/need to dynamically bind to ItemsSource. Is it possible to specify the datatype of ItemsSource in XAML (so that I can get intellisense)?

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Junjie Zhu - MSFT 15,756 Reputation points Microsoft Vendor
    2024-04-30T02:17:52.49+00:00

    Hi @杨岑

    Welcome to Microsoft Q&A!

    It is recommended to set AutoGenerateColumns="True", you can refer to this document How to: Customize Auto-Generated Columns in the DataGrid Control.

    Here is my test code for your reference.

    <Grid>
        <controls:DataGrid  Height="600" Margin="12"
    AutoGenerateColumns="True" AutoGeneratingColumn="DataGrid_AutoGeneratingColumn" ItemsSource="{x:Bind Persons}" />
    </Grid>
    
    public sealed partial class MainPage : Page
    {
           
        public List<Person> Persons { get; set; }
        public MainPage()
        {
            this.InitializeComponent();
    
            Persons = new List<Person>
            {
                new Person
                {
                    PersonId = 1, IsChecked = true, FirstName = "Ronald", LastName = "Rumple",
                    Position = "Network Administrator"
                },
                new Person
                {
                    PersonId = 2, IsChecked = false, FirstName = "Brett", LastName = "Banner",
                    Position = "Software Developer"
                },
                new Person
                {
                    PersonId = 3, IsChecked = true, FirstName = "Alice", LastName = "Anderson",
                    Position = "Accountant"
                }
            };
        }
    
        private void DataGrid_AutoGeneratingColumn(object sender, Microsoft.Toolkit.Uwp.UI.Controls.DataGridAutoGeneratingColumnEventArgs e)
        {
            //Modify the header of the "Name" column
            if (e.Column.Header.ToString() == "PersonId")
            {
                e.Column.Header = "ID";
            }
            if (e.Column.Header.ToString() == "IsChecked")
            {
                e.Column.Header = "Checked";
            }
        }
    }
    
    public class Person
    {
        public int PersonId { get; set; }
        public bool IsChecked { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Position { get; set; }
    }
    
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.