[UWP]新控制項ColorPicker

来源:https://www.cnblogs.com/dino623/archive/2018/02/04/ColorPicker.html
-Advertisement-
Play Games

1. 前言 Fall Creators Update中提供了一個新得ColorPicker控制項,解決了以前選擇顏色只能用Combo Box的窘境。 2. 一個簡單的例子 如上所示,ColorPiker可以通過在光譜或色輪上拖動滑塊,或者在RGB/HSV及十六進位的TextBox中直接輸入顏色的數值改 ...


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>

如上所示,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" />

3.3 其它屬性

使用如下XAML基本可以將所有元素顯示出來:

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

下麵列表列出了各元素對應的屬性。

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>

(也許是Flyout沒有添加陰影或邊框的原因,看起來醜醜的。)

5. 結語

Winform中有ColorDialog:

WPF有Extended WPF Toolkit 中的ColorPicker:

而UWP拖到現在才終於肯提供一個ColorPicker。每次更新技術都扔掉一些常用控制項,導致開發者只能選擇第三方控制項或自己實現,連TreeView都是拖了幾年才搞出來。這難道是微軟對我們的考驗嗎?

5. 參考

Color Picker
ColorPicker Class

6. 源碼

Fluent Design System Sample
XamlUIBasics


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一、Class類的使用 1.Java的反射機制是在編譯並不確定是哪個類被載入了,而是在程式運行的時候才載入、探知、自審。使用在編譯期並不知道的類。這樣的特點就是反射。 2.面向對象的世界里,萬物皆對象(除靜態的成員,普通數據類型不是對象),類是對象類,是java.lang.Class的實例對象3.怎 ...
  • Python版本:3.5.2 假如我們有一個Student類,併在其中定義了一個score屬性,但是score屬性會被顯露出去,沒辦法檢查參數,導致成績可以隨意更改: ~~~~ stu = Student() stu.score = 9999 ~~~~ 這顯然是不合邏輯的,為了限制score的範圍, ...
  • 一、dir內置函數 二、類 1、定義 類名 大駝峰命名法 2、創建 3、self參數(哪一個對象調用方法,self就是哪一個對象的引用 類似於this) (1)、增加屬性 (2)、 調用屬性 self.訪問對象的屬性 self.調用其他的對象方法 (PS 這種在外部賦值屬性的方法 不建議使用哦) ( ...
  • 登錄失敗信息回顯不會的新的一個頁面,而是顯示在登錄頁面 一種方法是: 登錄頁面表單中每個欄位後添加<span>標簽寫入失敗信息,利用ajax技術 通過改變<span>標簽的display:none屬性添加失敗信息 這裡用的類似的方法: 資料庫準備略, 註意寫好對應的user類供BeanHandler ...
  • package com.swift; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; impo... ...
  • lambda x: x * x,實際上就是 def f(x): retrun x * x 關鍵字lambda表示匿名函數,冒號前的x表示函數參數。 匿名函數只能有一個表達式,不用寫return,返回值就是該表達式的結果。 用匿名函數有個好處,因為函數沒有名字,不必擔心函數名衝突。此外,匿名函數也是一 ...
  • 微服務,通常都是用複雜的、大規模分散式集群來實現的。微服務構建在不同的軟體模塊上,這些軟體模塊,有可能是由不同的團隊開發、可能使用不同的編程語言來實現、有可能布在了幾千台伺服器,橫跨多個不同的數據中心。因此,就需要一些可以幫助理解系統行為、用於分析性能問題的工具。 API網關Ocelot 作為微服務 ...
  • 重寫 class 的 ToString() 來簡化獲取 enum 的 DescriptionAttribute 值 目錄 一、常見的 enum 類型版本 二、演變:class 版本的 enum 類型 三、演進:class 和 enum 兩者共存的版本 一、常見的 enum 類型版本 新建一個 Alg ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...