0

我正在使用电话应用程序,它有这样的情况,例如当记录未接电话或未接电话时,电话号码应该在列表框中显示为红色,并且当该号码被选择更改时,它应该恢复正常项目的前景色。

xml:

<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 实现它还是我需要编码?

谢谢,湿婆

4

1 回答 1

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>
    
于 2013-06-06T10:54:10.700 回答