On item selection, changing the color of a rectangle which is part of a ListBoxItem in Windows Phone

时间秒杀一切 提交于 2019-12-24 19:23:44

问题


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>

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?


回答1:


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);
}


来源:https://stackoverflow.com/questions/10907977/on-item-selection-changing-the-color-of-a-rectangle-which-is-part-of-a-listboxi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!