I want to create a new User Control which displays my own combobox. (The Combobox has a own style and other things…. (that comes later))
usercontrol xaml file:
<UserControl x:Class="WpfApplication1.MyCombobox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" d:DesignWidth="500"
Height="50">
<ComboBox Height="42" ItemsSource="{Binding Path=MyItems}"></ComboBox>
</UserControl>
usercontrol code behind file:
namespace WpfApplication1
{
public partial class MyCombobox
{
public MyCombobox()
{
InitializeComponent();
MyItems = new List<ComboBoxItem>();
}
public List<ComboBoxItem> MyItems { get; set; }
}
}
mainwindow xaml file:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfApplication1="clr-namespace:WpfApplication1" WindowStartupLocation="CenterScreen"
Height="350"
Width="500"
>
<Grid>
<WpfApplication1:MyCombobox>
<WpfApplication1:MyCombobox.MyItems>
<ComboBoxItem Height="36">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="16" />
<RowDefinition Height="16" />
</Grid.RowDefinitions>
<TextBlock Text="Item Title 1" Grid.Row="0" FontWeight="Bold" />
<TextBlock Text="Item Description 1" Grid.Row="1" FontStyle="Italic" />
</Grid>
</Grid>
</ComboBoxItem>
<ComboBoxItem Height="36">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="16" />
<RowDefinition Height="16" />
</Grid.RowDefinitions>
<TextBlock Text="Item Title 2" Grid.Row="0" FontWeight="Bold" />
<TextBlock Text="Item Description 2" Grid.Row="1" FontStyle="Italic" />
</Grid>
</Grid>
</ComboBoxItem>
</WpfApplication1:MyCombobox.MyItems>
</WpfApplication1:MyCombobox>
</Grid>
</Window>
I wish that I can add, in the main window, the comboboxitems to my usercontrol. Like that:
<WpfApplication1:MyCombobox.MyItems>
<ComboBoxItem Height="36">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="16" />
<RowDefinition Height="16" />
</Grid.RowDefinitions>
<TextBlock Text="Item Title 2" Grid.Row="0" FontWeight="Bold" />
<TextBlock Text="Item Description 2" Grid.Row="1" FontStyle="Italic" />
</Grid>
</Grid>
</ComboBoxItem>
<!-- more items... -->
</WpfApplication1:MyCombobox.MyItems>
The UserControl combobox used the Items which are passed in the mainwindow.
When I run the code, it only displays an empty combobox
What’s wrong?
This has to do with the way you’re creating the binding to MyItems in the Combobox.
To make it properly target the MyItems property in the code-behind, you can do the following: