【发布时间】:2012-01-01 23:38:52
【问题描述】:
我正在尝试格式化我的 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 Name="listBox_LibAll" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ListBoxItem Content="Item 1" />
<ListBoxItem Content="Item 2" />
<ListBoxItem Content="Item 3" />
</ListBox>
输出:
如果你看图片你会注意到图片有一个地方,我只是不知道如何设置它的值。我在想我可以以某种方式将“源”属性附加到 ListBoxItem
【问题讨论】:
标签: wpf xaml wpf-controls