8,735
社区成员
发帖
与我相关
我的任务
分享
<Window.Resources>
<Converters:ItemBackgroundConverter x:Key="ItemBackgroundConverter"/>
<!--ListBoxItem style -->
<Style TargetType="ListBoxItem">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="ItemGrid">
<ContentPresenter/>
<Border.Style>
<Style>
<Setter Property="Border.Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected, Converter={StaticResource ItemBackgroundConverter}}" />
<Setter Property="Border.BorderBrush" Value="Black" />
<Setter Property="Border.BorderThickness" Value="0.5" />
<Setter Property="Border.CornerRadius" Value="4" />
<Setter Property="Border.Margin" Value="2" />
<Setter Property="Border.Padding" Value="4" />
</Style>
</Border.Style>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<StackPanel>
<ListBox>
<ListBoxItem>Item1</ListBoxItem>
<ListBoxItem>Item2</ListBoxItem>
<ListBoxItem>Item3</ListBoxItem>
<ListBoxItem>Item4</ListBoxItem>
<ListBoxItem>Item5</ListBoxItem>
<ListBoxItem>Item6</ListBoxItem>
<ListBoxItem>Item7</ListBoxItem>
</ListBox>
</StackPanel>
</Grid>
public class ItemBackgroundConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Boolean)
{
Boolean blnChecked = (Boolean)value;
if (blnChecked)
{
return Color.FromArgb(0,255,0);
}
else
{
return Color.Green;
}
}
return Color.Green;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
Public Class MyConverter
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
Dim d As Data = CType(value, Data)
If d.Name.StartsWith("a") Then
Return New SolidColorBrush(Colors.Green)
Else
Return New SolidColorBrush(Colors.Yellow)
End If
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
Return Nothing
End Function
End Class