[UWP]新控件ColorPicker

简介: 原文:[UWP]新控件ColorPicker1. 前言 Fall Creators Update中提供了一个新得ColorPicker控件,解决了以前选择颜色只能用Combo Box的窘境。 2. 一个简单的例子 如上所示,ColorPiker可以通过在光谱或色轮上拖动滑块,或者在RGB/HSV及十六进制的TextBox中直接输入颜色的数值改变Color属性。
原文: [UWP]新控件ColorPicker

1. 前言

Fall Creators Update中提供了一个新得ColorPicker控件,解决了以前选择颜色只能用Combo Box的窘境。

2. 一个简单的例子

<ColorPicker x:Name="ColorPicker"
             Margin="5" />

<Grid Margin="5">
    <Grid.Background>
        <SolidColorBrush Color="{x:Bind ColorPicker.Color, Mode=OneWay}" />
    </Grid.Background>
    <TextBlock Text="{x:Bind ColorPicker.Color}" />
</Grid>

img_f15113783e9e20a6116609798a5e625d.png

如上所示,ColorPiker可以通过在光谱或色轮上拖动滑块,或者在RGB/HSV及十六进制的TextBox中直接输入颜色的数值改变Color属性。

3. 定制ColorPicker

ColorPicker提供了很多属性以设置它的外观,下面介绍一些常用的属性。

3.1 ColorSpectrumShape

ColorSpectrumShape是定义ColorPicker外观的主要属性。当设置为ColorSpectrumShape.Box时显示正方形的光谱,设置为ColorSpectrumShape.Ring时显示为圆型的HSV色轮。

3.2 最简化显示

完整的ColorPicker实在太占空间,而且整个控件左边高右边低,很不平衡。使用以下设置可以隐藏ColorPreview及其它Text Box以最简化ColorPicker的显示,使它勉强正常一点。

<ColorPicker x:Name="ColorPicker"
             ColorSpectrumShape="Ring"
             IsColorPreviewVisible="False"
             IsColorChannelTextInputVisible="False"
             IsHexInputVisible="False" />

img_2793afd0eff8fea43eec6e9ce94243be.png

3.3 其它属性

使用如下XAML基本可以将所有元素显示出来:

<ColorPicker x:Name="ColorPicker"
         IsColorPreviewVisible="True"
         IsAlphaEnabled="True"
         IsMoreButtonVisible="True"/>

img_1533ea67c86ab356650fe2c7651ba7f4.png

下面列表列出了各元素对应的属性。
img_56b05cf547c1bd23a32dbb5ed3a53ba9.png

4. 封装ColorPicker

ColorPicker难用的地方在于它是个大块头,而且没有Header,摆在表单里面格格不入。官方文档里面还介绍了怎么把ColorPicker放在Button的Flyout里使用,都做到这样了还不如直接提供这个弹出控件。

为了使它更好用我把它简单地封装到一个弹出控件中。由于Picker控件通常都是指点击按钮弹出一个Popup或Flyout通过鼠标点击选择值的控件,例如DatePicker、TimePicker或者Extended WPF Toolkit 中的ColorPicker,UWP中的ColorPicker这个名称让我很为难,只好把自己封装的控件命名为ColorSelector。详细代码请见文章最后给出的Fluent Design System Sample源码。

<Style TargetType="local:ColorSelector">
    <Setter Property="IsTabStop"
            Value="False" />
    <Setter Property="FontFamily"
            Value="{ThemeResource ContentControlThemeFontFamily}" />
    <Setter Property="FontSize"
            Value="{ThemeResource ControlContentThemeFontSize}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:ColorSelector">
                <StackPanel x:Name="LayoutRoot"
                            Margin="{TemplateBinding Padding}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="HeaderContentPresenter"
                                                                   Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                                Value="{ThemeResource DatePickerHeaderForegroundDisabled}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="PopupStates">
                            <VisualState x:Name="PopupOpened" />
                            <VisualState x:Name="PopupClosed" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <local:HeaderedContentControl Header="{TemplateBinding Header}"
                                                  HeaderTemplate="{TemplateBinding HeaderTemplate}">
                        <ToggleButton x:Name="DateButton"
                                      DataContext="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},Path=Color}"
                                      IsEnabled="{TemplateBinding IsEnabled}"
                                      IsChecked="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=IsDropDownOpen,Mode=TwoWay}"
                                      HorizontalAlignment="Stretch"
                                      HorizontalContentAlignment="Stretch">
                            <ToggleButton.Content>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition />
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="Select A Color:" />
                                        <Rectangle Grid.Column="1"
                                                   Margin="5,0,0,0">
                                            <Rectangle.Fill>
                                                <SolidColorBrush Color="{Binding}" />
                                            </Rectangle.Fill>
                                        </Rectangle>
                                    </Grid>
                                </ToggleButton.Content>
                            <FlyoutBase.AttachedFlyout>
                                <Flyout Placement="Bottom"
                                        x:Name="Flyout">
                                    <Flyout.FlyoutPresenterStyle>
                                        <Style TargetType="FlyoutPresenter">
                                            <Setter Property="Padding"
                                                    Value="0" />
                                            <Setter Property="BorderThickness"
                                                    Value="0" />
                                            <Setter Property="Template">
                                                <Setter.Value>
                                                    <ControlTemplate TargetType="FlyoutPresenter">
                                                        <ContentPresenter Background="{TemplateBinding Background}"
                                                                          BorderBrush="{TemplateBinding BorderBrush}"
                                                                          BorderThickness="{TemplateBinding BorderThickness}"
                                                                          Content="{TemplateBinding Content}"
                                                                          ContentTemplate="{TemplateBinding ContentTemplate}"
                                                                          ContentTransitions="{TemplateBinding ContentTransitions}"
                                                                          Margin="{TemplateBinding Padding}"
                                                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </Flyout.FlyoutPresenterStyle>
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition />
                                            <RowDefinition Height="Auto" />
                                        </Grid.RowDefinitions>
                                        <ColorPicker x:Name="ColorPicker"
                                                     Style="{TemplateBinding ColorPickerStyle}"
                                                     IsColorPreviewVisible="False"
                                                     IsColorChannelTextInputVisible="False"
                                                     IsHexInputVisible="False" />
                                        <Grid Grid.Row="1"
                                              Height="45"
                                              x:Name="AcceptDismissHostGrid">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*" />
                                                <ColumnDefinition Width="*" />
                                            </Grid.ColumnDefinitions>
                                            <Rectangle Height="2"
                                                       VerticalAlignment="Top"
                                                       Fill="{ThemeResource DatePickerFlyoutPresenterSpacerFill}"
                                                       Grid.ColumnSpan="2" />
                                            <Button x:Name="AcceptButton"
                                                    Grid.Column="0"
                                                    Content="&#xE8FB;"
                                                    FontFamily="{ThemeResource SymbolThemeFontFamily}"
                                                    FontSize="16"
                                                    HorizontalAlignment="Stretch"
                                                    VerticalAlignment="Stretch"
                                                    Style="{StaticResource DateTimePickerFlyoutButtonStyle}"
                                                    Margin="0,2,0,0" />
                                            <Button x:Name="DismissButton"
                                                    Grid.Column="1"
                                                    Content="&#xE711;"
                                                    FontFamily="{ThemeResource SymbolThemeFontFamily}"
                                                    FontSize="16"
                                                    HorizontalAlignment="Stretch"
                                                    VerticalAlignment="Stretch"
                                                    Style="{StaticResource DateTimePickerFlyoutButtonStyle}"
                                                    Margin="0,2,0,0" />
                                        </Grid>
                                    </Grid>
                                </Flyout>
                            </FlyoutBase.AttachedFlyout>
                        </ToggleButton>
                    </local:HeaderedContentControl>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

img_4d62e0582058b4b3fa9c6c98785759df.png

(也许是Flyout没有添加阴影或边框的原因,看起来丑丑的。)

5. 结语

Winform中有ColorDialog:
img_b70befe09be62a0e24528122e5150751.png

WPF有Extended WPF Toolkit 中的ColorPicker:
img_25ff7812dadb7f8014aacf4f6ea83daa.png

而UWP拖到现在才终于肯提供一个ColorPicker。每次更新技术都扔掉一些常用控件,导致开发者只能选择第三方控件或自己实现,连TreeView都是拖了几年才搞出来。这难道是微软对我们的考验吗?

5. 参考

Color Picker
ColorPicker Class

6. 源码

Fluent Design System Sample
XamlUIBasics

目录
相关文章
|
11月前
|
C# 容器
WPF技术之StatusBar控件
WPF StatusBar控件用于在应用程序底部显示状态信息。它提供了一个容器,用于展示与应用程序相关的各种状态信息。
505 0
|
前端开发 Windows
[UWP]使用Picker实现一个简单的ColorPicker弹窗
原文:[UWP]使用Picker实现一个简单的ColorPicker弹窗 在上一篇博文《[UWP]使用Popup构建UWP Picker》中我们简单讲述了一下使用Popup构建适用于MVVM框架下的弹窗层组件Picker的过程。
1116 0
|
前端开发 Windows 数据可视化
[UWP]使用Popup构建UWP Picker
原文:[UWP]使用Popup构建UWP Picker 在上一篇博文《[UWP]不那么好用的ContentDialog》中我们讲到了ContentDialog在复杂场景下使用的几个令人头疼的弊端。那么,就让我们在这篇博文里开始愉快的造轮子之旅吧! 首先要向大家说明:这篇博文主要还是写的构建Picker时的思考过程,如果不感兴趣的,可以直接略过这篇,阅读下一篇《[UWP]如何使用Picker实现一个简单的ColorPicker弹窗》。
1001 0
|
C# Windows
[UWP]实现Picker控件
原文:[UWP]实现Picker控件 1. 前言 在WPF中,很多打开下拉框(Popup或Flyout)选择一个结果值的控件,除了ComboBox等少数例外,这种控件都以-Picker做名称后缀。因为要打开关闭下拉框和计算下拉框的弹出位置, 这类控件实现起来还挺麻烦的。
835 0
|
JavaScript C#
wpf CefSharp 与 js交互
原文:wpf CefSharp 与 js交互 通过 NuGet 获取 CefSharp.WpF 组件。  xmlns:cefSharp="clr-namespace:CefSharp.
3857 0
|
Web App开发 算法 C#
WPF 控件库——仿制Chrome的ColorPicker
原文:WPF 控件库——仿制Chrome的ColorPicker 一、观察   项目中的一个新需求,需要往控件库中添加颜色拾取器控件,因为公司暂时还没有UI设计大佬入住,所以就从网上开始找各种模样的ColorPicker,找来找去我就看上了谷歌浏览器自带的,它长这个样:         看上去不错,可以搞!搞之前得观察一下这里面可能的一些坑。
1420 0
UWP UserControl 不会自适应大小
原文:UWP UserControl 不会自适应大小 在一般的Page里面,我们通过VisualStateManager,可以根据窗体的宽度,来调整一些控件大小。 ...
1316 0
UWP 使用Telerik Chart控件
原文:UWP 使用Telerik Chart控件 Telerik开发的chart功能异常强大 用户可以自行在商店搜索“UI for uwp demos”。     下面我就结合以下我的软件,来说明一下饼状图的实现。
1313 0
UWP 颜色选择器(ColorPicker) 和 自定义的Flyout(AdvancedFlyout)
原文:UWP 颜色选择器(ColorPicker) 和 自定义的Flyout(AdvancedFlyout) ColorPicker 故事背景 项目里面需要一个像Winfrom里面那样的颜色选择器,如下图所示: 在网上看了一下。
1467 0
|
虚拟化 容器 .NET
[UWP]了解模板化控件(8):ItemsControl
原文:[UWP]了解模板化控件(8):ItemsControl 1. 模仿ItemsControl 顾名思义,ItemsControl是展示一组数据的控件,它是UWP UI系统中最重要的控件之一,和展示单一数据的ContentControl构成了UWP UI的绝大部分,ComboBox,ListBox,ListView,FlipView,GridView等控件都继承自ItemsControl。
1369 0

热门文章

最新文章