简体   繁体   中英

User control in Data template not shown

I have this ComboBox

<ComboBox x:Name="Renderer" ItemsSource="{Binding RendererItems}" 
      ItemTemplate="{StaticResource DropDownItemTemplate}" SelectedIndex="0">
      <i:Interaction.Triggers>
          <i:EventTrigger EventName="SelectionChanged">
            <ei:CallMethodAction TargetObject="{Binding}" 
             MethodName="RendererItemsSelectionChanged"/>                               
          </i:EventTrigger>
      </i:Interaction.Triggers>
</ComboBox>

And this Data Template for the items

        <DataTemplate x:Key="DropDownItemTemplate">
            <StackPanel Orientation="Horizontal">
                <UserControl Content="{Binding Icon}" Width="24" Height="24"/>
                <TextBlock Text="{Binding Text}" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,0"/>
            </StackPanel>
        </DataTemplate>

And the data comes from:

public ObservableCollection<ComboBoxItemModel> RendererItems { get; set; } = new ObservableCollection<ComboBoxItemModel>();
        public MainWindowViewModel()
        {
            RendererItems.Add(new ComboBoxItemModel() { Icon = new RenderedIcon(), Text = "Rendered" });
            RendererItems.Add(new ComboBoxItemModel() { Icon = new WireframeIcon(), Text = "Wireframe" });
            RendererItems.Add(new ComboBoxItemModel() { Icon = new ShadedIcon(), Text = "Shaded" });
            RendererItems.Add(new ComboBoxItemModel() { Icon = new HiddenLinesIcon(), Text = "Hidden Lines" });
        }

The ComboBoxItemModel class is defined like this:

    public class ComboBoxItemModel
    {
        public UserControl Icon { get; set; }
        public string Text { get; set; }        
    }

The first time that I click on the Combo is shown like this:

在此处输入图像描述

As you can see the selected item have no Icon

The second time that I click on the Combo is shown like this:

在此处输入图像描述

Now the item that I have selected have no Icon. But I want that Combo items always have an Icon.

A UIElement - like your UserControl Icon - can only have one parent element and can therefore only appear once in a visual tree. You should not have a UIElement at all as a view model data item.

In order to model an icon, use a bitmap or a drawing in a DrawingImage:

public class ComboBoxItemModel
{
    public ImageSource Icon { get; set; } // assign a DrawingImage
    public string Text { get; set; }        
}

with

<DataTemplate x:Key="DropDownItemTemplate">
    <StackPanel Orientation="Horizontal">
        <Image Source="{Binding Icon}" Width="24" Height="24"/>
        <TextBlock Text="{Binding Text}" .../>
    </StackPanel>
</DataTemplate>

An alternative with a UserControl Icon might be to fill a Rectangle with a VisualBrush:

<DataTemplate x:Key="DropDownItemTemplate">
    <StackPanel Orientation="Horizontal">
        <Rectangle Width="24" Height="24">
            <Rectangle.Fill>
                <VisualBrush Visual="{Binding Icon}"/>
            </Rectangle.Fill>
        </Rectangle>
        <TextBlock Text="{Binding Text}" .../>
    </StackPanel>
</DataTemplate>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM