microsoft-ui-xaml icon indicating copy to clipboard operation
microsoft-ui-xaml copied to clipboard

Binding in DataTemplate for ComboBox items not working correctly

Open Crumbler opened this issue 2 years ago • 2 comments

Describe the bug

I'm writing a WinUI 3 app and am making a dialog in which the user can select one of multiple object types.

For the ComboBox's ItemTemplate I'm using a TextBlock whose Text is bound to the Name property (to display the Type's Name).

However, setting the ItemsSource to an array of Type causes the application to crash when opening the dropdown menu of the ComboBox with an exception of type Microsoft.Ui.Xaml.UnhandledException. Setting the SelectedIndex property via code also causes the application to crash.

Steps to reproduce the bug

  1. Create a new WinUI 3 App.
  2. Use the following sample page and code:
<Page
    x:Class="TestApp.ChooseObjectTypePage"
    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"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <ComboBox x:Name="comboBox" HorizontalAlignment="Stretch">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name, Mode=OneTime}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</Page>
using Microsoft.UI.Xaml.Controls;
using System;

namespace TestApp
{
    public sealed partial class ChooseObjectTypePage : Page
    {
        public ChooseObjectTypePage()
        {
            this.InitializeComponent();

            comboBox.ItemsSource = new Type[] { typeof(string) };
        }
    }
}
  1. Run the app.
  2. Try to open the ComboBox.

Expected behavior

The ComboBox items consist of the Types' Names.

Screenshots

No response

NuGet package version

No response

Windows app type

  • [X] UWP
  • [ ] Win32

Device form factor

Desktop

Windows version

Windows 10 (20H2): Build 19042

Additional context

No response

Crumbler avatar Jul 25 '22 12:07 Crumbler

still not fixed, 18 days. 😢

shelllet avatar Sep 01 '22 11:09 shelllet

The control is working fine, but the binding seems to not be happy with System.Type. You should be able to wrap it in C# class with a name property to workaround.

ranjeshj avatar Sep 16 '22 22:09 ranjeshj

When will it be fixed?

shelllet avatar Nov 25 '22 08:11 shelllet

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Jul 28 '23 22:07 github-actions[bot]