起初,我試圖用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>
現在,顏色已設置,選中時必須重置爲默認值。這可以通過多種方式來完成:
使用代碼:
private void ListBoxItem_Selected(object sender, RoutedEventArgs e)
{
ListBoxItem MyListBoxItem = sender as ListBoxItem;
Brush DefaultColor = this.Resources["DefaultForegroundColor"] as Brush;
DependencyPhoneClass.SetColorOfState(MyListBoxItem, DefaultColor);
}
或者使用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>
我在Windows應用商店的應用程序工作其中PropertyMetadata位於Windows.UI.Xaml,它會爲您的代碼引發以下錯誤,「Erro1:無法從」方法組「轉換爲」對象「Error2:最佳重載會見hord匹配'Windows.UI.Xaml.PropertyMetadata.PropertyMetadata(object)'有一些無效參數「和本地:DependencyPhoneClass.ColorOfState =」{StaticResource MissedForegroundColor}「顯示錯誤」名稱「DependencyPhoneClass」不存在於命名空間「使用:ListBoxTest「即使我添加這個」xmlns:local =「使用:ListBoxTest」「 – Sivakumarc
我試過在WPF中,這個工程很棒:) – Sivakumarc
不幸的是,我沒有在** Windows商店應用**下做這個示例,所以我不能確切地說。最有可能的是,你必須挖掘**依賴屬性**的一面。我試圖在這種情況下使用** VisualStateManager **,但後來被拒絕,因爲屬性更簡單。 –