在ListBox中更改了选择的颜色更改

时间:2013-06-05 11:50:53

标签: wpf xaml listbox windows-store-apps listboxitem

我正在使用电话应用程序,其中有一种情况,例如记录未接来电或未应答的电话,电话号码应在列表框中显示为红色,当该号码被选择更改时,它应该回到正常项目的前景色。

的Xaml:

<ListBox x:Name="ListBox1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="370" ItemsSource="{Binding AllMissedCalls}" ItemContainerStyle="{StaticResource ListBoxItemStyle1}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="Hello"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
 </ListBox>

我可以用VisualStates实现它,还是需要代码?

谢谢, 希瓦

1 个答案:

答案 0 :(得分:0)

起初我尝试使用 VisualStateManger ,但后来我决定采用不同的方法。我创建了一个依赖项属性,它存储它会突出显示颜色,它可以像这样使用:

<ListBoxItem Name="Missed" local:DependencyPhoneClass.ColorOfState="{StaticResource MissedForegroundColor}">

依赖属性类的代码:

public class DependencyPhoneClass : DependencyObject
{
    public static DependencyProperty ColorOfStateProperty;

    public static void SetColorOfState(DependencyObject DepObject, Brush value)
    {
        DepObject.SetValue(ColorOfStateProperty, value);
    }

    public static Brush GetColorOfState(DependencyObject DepObject)
    {
        return (Brush)DepObject.GetValue(ColorOfStateProperty);
    }

    static DependencyPhoneClass()
    {
        ColorOfStateProperty = DependencyProperty.RegisterAttached("CollorOfState",
                                                            typeof(Brush),
                                                            typeof(DependencyPhoneClass),
                                                            new PropertyMetadata(OnColorStateNameChanged));
    }

    private static void OnColorStateNameChanged(object sender, DependencyPropertyChangedEventArgs args)
    {
        var MyListBoxItem = sender as ListBoxItem;

        if (MyListBoxItem == null)
        {
            throw new InvalidOperationException("This attached property only supports types derived from Control");
        }

        Brush ColorOfState = GetColorOfState(MyListBoxItem);

        if (ColorOfState != null)
        {
            MyListBoxItem.Foreground = ColorOfState;
        }
    }
}

我在资源中创建了颜色:

<Window.Resources>
    <SolidColorBrush x:Key="DefaultForegroundColor" Color="Black" />
    <SolidColorBrush x:Key="MissedForegroundColor" Color="Red" />
    <SolidColorBrush x:Key="UnansweredForegroundColor" Color="OrangeRed" />
</Window.Resources>

列表框:

<ListBox Name="PhoneListBox" HorizontalAlignment="Center" VerticalAlignment="Top" Width="370" Height="100">
    <ListBoxItem Name="Missed" local:DependencyPhoneClass.ColorOfState="{StaticResource MissedForegroundColor}" Content="Daddy: 1" />
    <ListBoxItem Name="Unanswered" local:DependencyPhoneClass.ColorOfState="{StaticResource UnansweredForegroundColor}" Content="Mom: 15" />
    <ListBoxItem Name="Normal" local:DependencyPhoneClass.ColorOfState="{StaticResource DefaultForegroundColor}" Content="Kim: 0" />
</ListBox> 

现在,颜色已设置,选中时必须将其重置为默认值。这可以通过以下几种方式完成:

  1. 使用代码:

    private void ListBoxItem_Selected(object sender, RoutedEventArgs e)
    {
        ListBoxItem MyListBoxItem = sender as ListBoxItem;
        Brush DefaultColor = this.Resources["DefaultForegroundColor"] as Brush;
    
        DependencyPhoneClass.SetColorOfState(MyListBoxItem, DefaultColor);
    }
    
  2. 或者在XAML中使用EventTrigger:

    <ListBoxItem.Triggers>
        <EventTrigger RoutedEvent="ListBoxItem.Selected">
            <BeginStoryboard>
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Missed" Storyboard.TargetProperty="Foreground">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource DefaultForegroundColor}" />
                     </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </ListBoxItem.Triggers>