In the Loaded window handler, I create a ComboBox and add it to the Grid window. When you start the ComboBox application, it draws incorrectly (Fig. 1) and is displayed so until I hover the mouse on it (Fig. 2).

Why is this happening and how can this be fixed by leaving the creation and addition of a ComboBox in the Loaded handler?

XAML:

 <Window x:Class="WpfTest.MainWindow" xmlns="schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft.com/winfx/2006/xaml" SizeToContent="WidthAndHeight"> <Grid x:Name="mainGrid" /> </Window> 

Code behind:

 public MainWindow() { InitializeComponent(); Loaded += MainWindow_Loaded; } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { var comboBox = new ComboBox { Width = 100, Margin = new Thickness(50) }; mainGrid.Children.Add(comboBox); comboBox.Items.Add(new TextBlock { Text = "Item 1" }); comboBox.Items.Add(new TextBlock { Text = "Item 2" }); comboBox.Items.Add(new TextBlock { Text = "Item 3" }); comboBox.SelectedIndex = 1; } 

enter image description here enter image description here

Upd: How to make the installation SelectedIndex immediately select the specified item?

  • Try the Children.Add operation to move to the very end of the code, after selecting SelectedIndex . - Monk
  • Unfortunately, it did not help - Vitaliy
  • Is this all code? Do not handle any more events? - Donil 4:19 pm
  • Yes, this is all the code in a separately created empty project - Vitaliy
  • Maybe make a refresh window? - isnullxbh

2 answers 2

The problem is that the selected item is not displayed in the combo box until you hover the mouse. Because of this, the combo box flattens, and behind it the window is flattened. If you remove the SizeToContent="WidthAndHeight" , then you can see the window of normal size and combo box without the text of the selected item.

The selected item is displayed only if SelectedIndex set out of the Loaded event (for example, in the window constructor or in the button click handler) or if strings are added as items in the Loaded event and not controls. To be honest, it looks like a WPF bug and can only manifest itself on certain versions of the framework / OS.

  • Obviously, the question is still why the selected item is not shown! In my case, it can be any FrameworkElement. Text, picture, button, video, table - whatever, not the point. - Vitaliy
  • This "answer" is not the answer. This is just a way to get away from the question. The issue has not yet been resolved and is still relevant. - Vitaliy
  • @Vitaliy updated the answer, but, alas, with no results. - andreycha

Everything compiled without problems for me, perhaps pluggable namespaces affect the drawing of elements, although it may all depend on the version. I compiled on Visual Studio 2015 sp3

 <Window x:Class="WpfApplication1.MainWindow" 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" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" SizeToContent="WidthAndHeight"> <Grid x:Name="mainGrid" /> </Window> using System; using System.Windows; using System.Windows.Controls; namespace WpfApplication1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { var comboBox = new ComboBox { Width = 100, Margin = new Thickness(50) }; mainGrid.Children.Add(comboBox); comboBox.Items.Add(new TextBlock { Text = "Item 1" }); comboBox.Items.Add(new TextBlock { Text = "Item 2" }); comboBox.Items.Add(new TextBlock { Text = "Item 3" }); comboBox.SelectedIndex = 1; } } } 
  • And what is your OS, version of the framework, version of the project? - andreycha
  • Windows 10.0.10586, Visual Studio Enterprise 2015 14.0.25424.00 Update 3, .NET 4.5 project, framework v4.0.30319 - Vitaliy