我注意到默认情况下,在ListPicker中,边框和当前所选项目的文本会自动成为手机的强调颜色。我想知道如何将这些值硬编码为另一种颜色(将在ListPicker本身中选择)。例如,我的手机主题是石灰,但我想在选择钴项目时手动将边框和突出显示的文本颜色设置为钴。青色,红色等相同。我的ListPicker看起来像这样
XAML
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Name="ListPickerItemTemplate">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Image}" Width="50" Height="37.59"/>
<TextBlock Text="{Binding Name}" Margin="12,0,0,0" TextWrapping="Wrap"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Name="ListPickerFullModeItemTemplate">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Image}" Width="50" Height="37.59"/>
<TextBlock Text="{Binding Name}" Margin="12,0,0,0" TextWrapping="Wrap"/>
</StackPanel>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
...
<toolkit:ListPicker x:Name="themeListPicker" Header="Theme" FullModeHeader="Theme" CacheMode="BitmapCache"
SelectionChanged="themeListPicker_SelectionChanged"
ItemTemplate="{StaticResource ListPickerItemTemplate}"
FullModeItemTemplate="{StaticResource ListPickerFullModeItemTemplate}"/>
XAML.CS
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
themeList = new List<Theme>();
themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Indigo.png", UriKind.Relative)), Name = "indigo" });
//themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Lime.png", UriKind.Relative)), Name = "lime" });
//themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Green.png", UriKind.Relative)), Name = "green" });
//themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Emerald.png", UriKind.Relative)), Name = "emerald" });
//themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Teal.png", UriKind.Relative)), Name = "teal" });
themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Cyan.png", UriKind.Relative)), Name = "cyan" });
themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Cobalt.png", UriKind.Relative)), Name = "cobalt" });
//themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Violet.png", UriKind.Relative)), Name = "violet" });
//themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Pink.png", UriKind.Relative)), Name = "pink" });
//themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Magenta.png", UriKind.Relative)), Name = "magenta" });
//themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Crimson.png", UriKind.Relative)), Name = "crimson" });
themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Red.png", UriKind.Relative)), Name = "red" });
//themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Orange.png", UriKind.Relative)), Name = "orange" });
//themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Amber.png", UriKind.Relative)), Name = "amber" });
//themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Yellow.png", UriKind.Relative)), Name = "yellow" });
//themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Brown.png", UriKind.Relative)), Name = "brown" });
//themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Olive.png", UriKind.Relative)), Name = "olive" });
themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Steel.png", UriKind.Relative)), Name = "steel" });
//themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Mauve.png", UriKind.Relative)), Name = "mauve" });
//themeList.Add(new Theme() { Image = new BitmapImage(new Uri("/Assets/Themes/Sienna.png", UriKind.Relative)), Name = "sienna" });
themeListPicker.ItemsSource = themeList;
}
正如您所看到的,我的ListPicker中可能有超过5个项目,其中FullModeItemTemplate优先(尽管我最多只能坚持5个选项)。如何更改ListPickers边框和选定的项目强调颜色以匹配ListPicker中选择的颜色?
答案 0 :(得分:1)
你必须做几件事:
Color
和Brush
媒体资源添加到Theme
课程。ListPicker
的模板添加到您的xaml CurrentTheme
属性添加到xaml.cs CurrentTheme
绑定到ListPicker
您的主题类看起来像
public class Theme
{
public string Name { get; set; }
public BitmapImage Image { get; set; }
public Color Color { get; set; }
public SolidColorBrush Brush
{
get
{
return new SolidColorBrush(Color);
}
}
}
这样可以轻松地将彩色主题添加到列表中(使用Color属性)并将该颜色绑定到文本和边框(使用Brush属性)。
YourPage.xaml.cs
themeList.Add(new Theme
{
Image = new BitmapImage(new Uri("/Assets/Themes/Indigo.png", UriKind.Relative)),
Name = "cyan",
Color = Colors.Cyan
});
YourPage.xaml
<TextBlock
Text="{Binding Name}"
Foreground="{Binding Brush}"
Margin="12,0,0,0"
TextWrapping="Wrap"/>
如果要将所选主题绑定到ListPicker的边框,则需要为ListPicker添加模板。最简单的方法是使用Expression Blend。如果你已经这样做,你可以绑定边框的颜色,如下所示:
将当前主题添加到YourPage.xaml:
private Theme _currentTheme;
public Theme CurrentTheme
{
get
{
return _currentTheme;
}
private set
{
if (value == _currentTheme) return;
_currentTheme = value;
NotifyPropertyChanged("CurrentTheme");
}
}
private void ThemesListPicker_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ThemesListPicker.SelectedItem == null) return;
CurrentTheme = ThemesListPicker.SelectedItem as Theme;
}
将画笔绑定到列表选择器模板中的边框:
<!-- Omitted rest of template for brevity -->
<Border
x:Name="Border"
Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{Binding CurrentTheme.Brush}">
答案 1 :(得分:0)
认为您可以使用转换器来获得所需的结果
转换器
public class SelectedItemColorConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)value)
{
return new SolidColorBrush((Colors.Red);
}
return new SolidColorBrush(Colors.White);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}