问题描述
我有一个带有以下 XAML 的列表框:
I have a ListBox with the following XAML:
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Name="listItemGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition MaxWidth="20" MinWidth="20" Width="20" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Rectangle Name="listItemSideBar" Height="85" HorizontalAlignment="Left"
Margin="0, 0, 0, 0" Stroke="{StaticResource PhoneAccentBrush}"
StrokeThickness="1" VerticalAlignment="Top" Fill="{StaticResource
PhoneAccentBrush}" MinHeight="85" Width="25"/>
<StackPanel Margin="0,0,0,17" Grid.Column="1" Grid.ColumnSpan="2">
<TextBlock Name="listItemMainData" Text="{Binding LineTwo}"
TextWrapping="NoWrap" Margin="12,0,0,0" Style="{StaticResource
PhoneTextExtraLargeStyle}"/>
<TextBlock Name="listItemSubData" Text="{Binding LineOne}"
TextWrapping="NoWrap" Margin="12,-6,0,0"
Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
当一个 ListBoxItem 被选中时,我想将该 ListBoxItem 的矩形填充颜色更改为不同的颜色;当取消选择 ListBoxItem 时,我希望填充颜色改回 PhoneAccentBrush.
When a ListBoxItem is selected I would like to change that ListBoxItem's Rectangle fill color to a different color; when the ListBoxItem is deselected I would like the fill color to change back to PhoneAccentBrush.
有没有办法完成这个任务?
Is there a way to accomplish this task?
推荐答案
我终于找到了一个解决方案,我复制了下面的内容.ColorHelper
类只是从 PhoneAccentBrush 资源中获取 PhoneAccentColor.DarkPhoneAccentColor 是通过在此处找到的方法生成的.PropertyValue(child, "Name")
是一种语法糖扩展方法,用于从类型的属性中获取值;如果属性不存在,则返回 default(T)
.
I finally worked out a solution which I have copied below. The ColorHelper
class simply gets the PhoneAccentColor from the PhoneAccentBrush resource. The DarkPhoneAccentColor is produced via the method found here. PropertyValue<string>(child, "Name")
is a syntactic sugar extension method to get a value from a property of a type; it returns default(T)
if the property does not exist.
尚未对此代码应用完整的错误检查.
Full error checking has not yet been applied to this code.
private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Color accent = ColorHelper.PhoneAccentColor;
Color accentDark = ColorHelper.DarkPhoneAccentColor;
foreach (object item in e.RemovedItems)
SetListBoxItemColor(item, accentDark);
foreach (object item in e.AddedItems)
SetListBoxItemColor(item, accent);
}
private void SetListBoxItemColor(object item, Color color)
{
ListBoxItem listBoxItem = listBox.ItemContainerGenerator
.ContainerFromItem(item) as ListBoxItem;
if (listBoxItem != null)
{
Rectangle rectangle = GetItemsRecursive<Rectangle>(
listBoxItem, "listItemSideBar");
SolidColorBrush fillBrush = new SolidColorBrush();
fillBrush.Color = color;
rectangle.Fill = fillBrush;
}
}
private T GetItemsRecursive<T>(DependencyObject lb, string name)
where T : DependencyObject
{
int childrenCount = VisualTreeHelper.GetChildrenCount(lb);
for (int i = 0; i < childrenCount; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(lb, i);
if (child is T)
{
string childName = child.GetType().PropertyValue<string>(child, "Name");
if (String.Compare(childName, name) == 0)
return (T)child;
}
return GetItemsRecursive<T>(child, name);
}
return default(T);
}
这篇关于在项目选择时,更改作为 Windows Phone 中 ListBoxItem 一部分的矩形的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!