最近在做一个课程设计,主要做一个聊天软件,其中要做一个好友列表,虽然做的不是很精美,但是学习还是有用的。
这个模板主要是实现一个带有图标的ListBoxItem的效果
如下图:
这个ListBoxItem是写在APP.XML中的以个样式模板,每次添加的ListBoxItem都会应用到,动态的也会应用这个样式。模板在msdn里面有
下面是代码
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border
Name="Border"
Padding="2"
SnapsToDevicePixels="true">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Width="30" Height="30" Source="Img/header.ico" Grid.Column="0"/>
<ContentPresenter Grid.Column="1" VerticalAlignment="Center" ></ContentPresenter>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background"
Value="Blue"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground"
Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
引用代码
<ListBox>
<ListBoxItem>我去你妹的</ListBoxItem>
<ListBoxItem>我去你妹的</ListBoxItem>
<ListBoxItem>我去你妹的</ListBoxItem>
<ListBoxItem>我去你妹的</ListBoxItem>
</ListBox>
希望这段代码对正在学习WPF的同学有帮助