Skip to content

Switching ItemSource of different Structure causes residual binding error #286

@prakash-gnapitech

Description

@prakash-gnapitech

Description:

I have two or more Item Lists with different datatypes or the structure of the data, e.g.,

public record Contact
{
    public string Name { get; init; } = string.Empty;
    public string Email { get; init; } = string.Empty;
    public string Phone { get; init; } = string.Empty;
}

public record Person
{
    public string FirstName { get; init; } = string.Empty;
    public string LastName { get; init; } = string.Empty;
    public int Age { get; init; }
}

Now switching between the lists cause binding errors.

Error: BindingExpression path error: 'Email' property not found on 'Person { FirstName = FirstName 96, LastName = LastName 96, Age = 26 }'. BindingExpression: Path='Email' DataItem='Person { FirstName = FirstName 96, LastName = LastName 96, Age = 26 }'; target element is 'Microsoft.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String')
Error: BindingExpression path error: 'Phone' property not found on 'Person { FirstName = FirstName 96, LastName = LastName 96, Age = 26 }'. BindingExpression: Path='Phone' DataItem='Person { FirstName = FirstName 96, LastName = LastName 96, Age = 26 }'; target element is 'Microsoft.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String')
Error: BindingExpression path error: 'Name' property not found on 'Person { FirstName = FirstName 97, LastName = LastName 97, Age = 27 }'. BindingExpression: Path='Name' DataItem='Person { FirstName = FirstName 97, LastName = LastName 97, Age = 27 }'; target element is 'Microsoft.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String')
Error: BindingExpression path error: 'Email' property not found on 'Person { FirstName = FirstName 97, LastName = LastName 97, Age = 27 }'. BindingExpression: Path='Email' DataItem='Person { FirstName = FirstName 97, LastName = LastName 97, Age = 27 }'; target element is 'Microsoft.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String')
Error: BindingExpression path error: 'Phone' property not found on 'Person { FirstName = FirstName 97, LastName = LastName 97, Age = 27 }'. BindingExpression: Path='Phone' DataItem='Person { FirstName = FirstName 97, LastName = LastName 97, Age = 27 }'; target element is 'Microsoft.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String')
Error: BindingExpression path error: 'Name' property not found on 'Person { FirstName = FirstName 98, LastName = LastName 98, Age = 28 }'. BindingExpression: Path='Name' DataItem='Person { FirstName = FirstName 98, LastName = LastName 98, Age = 28 }'; target element is 'Microsoft.UI.Xaml.Controls.TextBlock' (Name='null'); 

Tried multiple ways to resolve like handle during datacontext switching or manual generation of the columns and many more. still no resolution. I could manage to switch the lists.
Any help will be appreciated

Steps to Reproduce:

one form of example code is here

# ViewModel
namespace TableViewTest
{
    public class MainViewModel : ObservableObject
    {
        public ObservableCollection<object> Items { get; set; } = [];


        public void LoadContacts()
        {
            Items = [];

            for (int i = 1; i <= 100; i++)
            {
                Items.Add(new Contact()
                {
                    Name = $"Contact {i}",
                    Email = $"email{i}.com".ToLower(),
                    Phone = $"123-456-78{i:D2}"
                });
            }

        }

        public void LoadPersons()
        {
            Items = [];

            for (int i = 1; i <= 100; i++)
            {
                Items.Add(new Person()
                {
                    FirstName = $"FirstName {i}",
                    LastName = $"LastName {i}",
                    Age = 20 + (i % 30)
                });
            }
        }
    }
}

# CS File

 MainViewModel ViewModel { get; } = new MainViewModel();
 public MainWindow()
 {
     InitializeComponent();
     ViewModel.LoadContacts();
     MyTableView.DataContext = ViewModel;
 }

 private void ContactsButton_Click(object sender, RoutedEventArgs e)
 {
     ViewModel.LoadContacts();
 }

 private void PersonsButton_Click(object sender, RoutedEventArgs e)
 {
     ViewModel.LoadPersons();
 }

The xaml behind

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <StackPanel Orientation="Horizontal"
                Margin="10">
        <Button x:Name="PersonsButton"
                Content="Persons"
                Margin="0,0,10,0"
                Click="PersonsButton_Click" />
        <Button x:Name="ContactsButton"
                Content="Contacts"
                Click="ContactsButton_Click" />
    </StackPanel>
    
    <!-- TableView Control -->
    <tv:TableView x:Name="MyTableView"
                  Grid.Row="1"
                  Margin="10" 
                  AutoGenerateColumns="True"
                  ItemsSource="{x:Bind ViewModel.Items, Mode=OneWay}"/>
</Grid>

Expected behavior:

No Binding errors

Screenshots:

Error already given above

Environment:

  • Package Version: 1.3.4
  • WinAppSDK Version: 1.8.251106002

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions