Я пытаюсь отформатировать свой шаблон ListBoxItem, чтобы включить изображение.Я могу добавить изображение в ListBoxItem, но я не совсем уверен, как бы я мог установить значение для этого изображения.
Шаблон для ListBoxItem:
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Padding="2" SnapsToDevicePixels="true">
<StackPanel Orientation="Horizontal" >
<Image Source="{Binding Path=Source}" Height="16" Width="16" HorizontalAlignment="Center" VerticalAlignment="Center" />
<ContentPresenter Name="ContentPresenter" HorizontalAlignment="Stretch" Width="Auto" />
</StackPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="{StaticResource ListBoxItem_BackgroundBrush_Selected}"/>
<Setter TargetName="ContentPresenter" Property="TextElement.FontWeight" Value="Bold"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{StaticResource TabItem_BackgroundBrush_Disabled}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Пример кода ListBox:
<ListBox Name="listBox_LibAll" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ListBoxItem Content="Item 1" />
<ListBoxItem Content="Item 2" />
<ListBoxItem Content="Item 3" />
</ListBox>
Вывод:
Если вы посмотрите на картинку, вы заметите, что для изображения есть место, я просто не знаю, как установить его значение.Я думал, что мог бы как-то присоединить свойство «Source» к ListBoxItem