microsoft-ui-xaml
microsoft-ui-xaml copied to clipboard
Binding in DataTemplate for ComboBox items not working correctly
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
- Create a new WinUI 3 App.
- 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) };
}
}
}
- Run the app.
- 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
still not fixed, 18 days. 😢
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.
When will it be fixed?
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.