microsoft / microsoft-ui-xaml

WinUI: a modern UI framework with a rich set of controls and styles to build dynamic and high-performing Windows applications.
MIT License
6.38k stars 683 forks source link

'x:Bind' not working inside DataTemplate's when using it inside a custom control. #8895

Open sudharsan-narayanan opened 1 year ago

sudharsan-narayanan commented 1 year ago

Describe the bug

When we use the ComboBox ItemsSource as x:bind inside the Data Template, it uses as custom control. The binding item source value is not rendering in the Combobox items. Please find the sample and video below for more details,

App15.zip

https://github.com/microsoft/microsoft-ui-xaml/assets/93642597/d746805e-960a-4d1d-a08c-6d9deee76092

In x:bind, it not works. (ItemsSource="{x:Bind local:MainViewModel.ComboBoxItems, Mode=OneWay}")

In Binding, it works. (ItemsSource="{Binding ComboBoxItems}")

Steps to reproduce the bug

  1. Run the sample.
  2. open the combobox.
  3. Note, ComboBox items are not render when we use it as x:bind in ItemsSource.

Expected behavior

Need to show the items in both x:bind and Binding

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.3.1: 1.3.230502000

Windows version

Windows 11 (21H2): Build 22000

Additional context

No response

ghord commented 1 year ago

You are using x:Bind in a wrong way. Your data template does not have x:DataType.

Also, your custom control uses contentpresenter without having content (it will always be null), and x:Bind mechanism does not work with null DataContexts.

This is not a bug in winui.

sudharsan-narayanan commented 1 year ago

In ComboBox ItemTemplate, we can't be able to use x:bind to bind the property's value for below code,

`<ComboBox x:Name="CountryComboBox"

ItemsSource="{x:Bind model.Countries}" >

        <ComboBox.ItemTemplate>

            <DataTemplate x:DataType="local:Country">

                <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

                    <Grid.ColumnDefinitions>

                        <ColumnDefinition Width="Auto" />

                        <ColumnDefinition Width="*" />

                    </Grid.ColumnDefinitions>

                    <ImageIcon

                Grid.Column="0"

                Width="24"

                Height="24"

                Source="{x:Bind CountryFlagPath}" />

                    <TextBlock

                Grid.Column="1" 

                VerticalAlignment="Center"

                Text="{x:Bind CountryName}" />

                </Grid>

            </DataTemplate>

        </ComboBox.ItemTemplate>

    </ComboBox>`
michael-hawker commented 11 hours ago

This is probably related also to #2508