0

私は電話アプリに取り組んでいます。これは、不在着信や不在着信が記録されたときに電話番号がリストボックスに赤く表示され、その番号の選択が変更されたときに通常の項目の前景色に戻るという状況があります。

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 で実装できますか、それともコーディングする必要がありますか?

ありがとう、シヴァ

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 に答える