2013-06-05 47 views
0

我正在使用電話應用程序,它有類似於未接電話或未接電話的情況,該電話號碼應該在列表框中顯示爲紅色,並且當該號碼被選擇更改時它應該回到正常項目的前景色。在ListBox中更改選擇的顏色更改

的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執行或者我需要編寫?

感謝, 西瓦

回答

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. 或者使用EventTrigger在XAML:

    <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> 
    
+0

我在Windows應用商店的應用程序工作其中PropertyMetadata位於Windows.UI.Xaml,它會爲您的代碼引發以下錯誤,「Erro1:無法從」方法組「轉換爲」對象「Error2:最佳重載會見hord匹配'Windows.UI.Xaml.PropertyMetadata.PropertyMetadata(object)'有一些無效參數「和本地:DependencyPhoneClass.ColorOfState =」{StaticResource MissedForegroundColor}「顯示錯誤」名稱「DependencyPhoneClass」不存在於命名空間「使用:ListBoxTest「即使我添加這個」xmlns:local =「使用:ListBoxTest」「 – Sivakumarc

+0

我試過在WPF中,這個工程很棒:) – Sivakumarc

+0

不幸的是,我沒有在** Windows商店應用**下做這個示例,所以我不能確切地說。最有可能的是,你必須挖掘**依賴屬性**的一面。我試圖在這種情況下使用** VisualStateManager **,但後來被拒絕,因爲屬性更簡單。 –