C# 如何更改ListPicker选择模板
我注意到,默认情况下,在ListPicker中,边框和当前选定项目的文本自动成为手机的强调色。我想知道如何将这些值硬编码为另一种颜色(将在ListPicker本身中选择)。例如,我的手机主题是石灰,但我想在选中钴项目时手动将边框和突出显示的文本颜色设置为钴。青色、红色等颜色也一样。我的列表选择器看起来像这样 XAMLC# 如何更改ListPicker选择模板,c#,xaml,windows-phone-7,windows-phone-8,listpicker,C#,Xaml,Windows Phone 7,Windows Phone 8,Listpicker,我注意到,默认情况下,在ListPicker中,边框和当前选定项目的文本自动成为手机的强调色。我想知道如何将这些值硬编码为另一种颜色(将在ListPicker本身中选择)。例如,我的手机主题是石灰,但我想在选中钴项目时手动将边框和突出显示的文本颜色设置为钴。青色、红色等颜色也一样。我的列表选择器看起来像这样 XAML ... XAML.CS protected override void OnNavigatedTo(NavigationEventArgs e) { b
...
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;
}
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
基地。导航到(e);
themeList=新列表();
添加(new Theme(){Image=new BitmapImage(newURI(“/Assets/Themes/Indigo.png”,UriKind.Relative)),Name=“Indigo”});
//添加(new Theme(){Image=new BitmapImage(new Uri(“/Assets/Themes/Lime.png”,UriKind.Relative)),Name=“Lime”});
//添加(new-Theme(){Image=new-BitmapImage(new-Uri(“/Assets/Themes/Green.png”,UriKind.Relative)),Name=“Green”});
//添加(new-Theme(){Image=new-BitmapImage(new-Uri(“/Assets/Themes/Emerald.png”,UriKind.Relative)),Name=“Emerald”});
//添加(new Theme(){Image=new BitmapImage(newURI(“/Assets/Themes/Teal.png”,UriKind.Relative)),Name=“Teal”});
添加(new Theme(){Image=new BitmapImage(newURI(“/Assets/Themes/Cyan.png”,UriKind.Relative)),Name=“Cyan”});
添加(new Theme(){Image=new BitmapImage(new Uri(“/Assets/Themes/Cobalt.png”,UriKind.Relative)),Name=“Cobalt”});
//添加(new Theme(){Image=new BitmapImage(new Uri(“/Assets/Themes/Violet.png”,UriKind.Relative)),Name=“Violet”});
//添加(new Theme(){Image=new BitmapImage(new Uri(“/Assets/Themes/Pink.png”,UriKind.Relative)),Name=“Pink”});
//添加(new-Theme(){Image=new-BitmapImage(新Uri(“/Assets/Themes/Magenta.png”,UriKind.Relative)),Name=“Magenta”});
//添加(new Theme(){Image=new BitmapImage(newURI(“/Assets/Themes/Crimson.png”,UriKind.Relative)),Name=“Crimson”});
添加(new-Theme(){Image=new-BitmapImage(new-Uri(“/Assets/Themes/Red.png”,UriKind.Relative)),Name=“Red”});
//添加(new-Theme(){Image=new-BitmapImage(new-Uri(“/Assets/Themes/Orange.png”,UriKind.Relative)),Name=“Orange”});
//添加(new Theme(){Image=new BitmapImage(newURI(“/Assets/Themes/Amber.png”,UriKind.Relative)),Name=“Amber”});
//添加(new-Theme(){Image=new-BitmapImage(new-Uri(“/Assets/Themes/Yellow.png”,UriKind.Relative)),Name=“Yellow”});
//添加(new Theme(){Image=new BitmapImage(new Uri(“/Assets/Themes/Brown.png”,UriKind.Relative)),Name=“Brown”});
//添加(new-Theme(){Image=new-BitmapImage(new-Uri(“/Assets/Themes/Olive.png”,UriKind.Relative)),Name=“Olive”});
添加(new Theme(){Image=new BitmapImage(new Uri(“/Assets/Themes/Steel.png”,UriKind.Relative)),Name=“Steel”});
//添加(new Theme(){Image=new BitmapImage(new Uri(“/Assets/Themes/Mauve.png”,UriKind.Relative)),Name=“Mauve”});
//添加(new Theme(){Image=new BitmapImage(newURI(“/Assets/Themes/Sienna.png”,UriKind.Relative)),Name=“Sienna”});
themelisticker.ItemsSource=themeList;
}
正如您所看到的,我的ListPicker中可能有5个以上的项,其中FullModeItemTemplate优先(尽管我最多只能使用5个选项)。如何更改ListPicker边框和所选项目强调色以匹配ListPicker中所选的颜色?您认为可以使用转换器获得所需的结果吗 用于转换器
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;
}
}
你必须做几件事:
颜色
和画笔
属性添加到主题
类中列表选择器的模板添加到xaml中
CurrentTheme
属性添加到xaml.csCurrentTheme
绑定到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);
}
}
}
这使得将彩色主题添加到列表(使用“颜色”属性)并将该颜色绑定到文本和边框(使用“笔刷”属性)变得很容易
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"/>
将笔刷绑定到列表选择器模板中的边框:
<!-- Omitted rest of template for brevity -->
<Border
x:Name="Border"
Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{Binding CurrentTheme.Brush}">
您能解释一下如何让ListPicker模板能够更改笔刷吗?我记得以前在Visual Studio中这样做过,但我无法追溯我的步骤。右键单击解决方案资源管理器中的page.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}">