C# wpf簡(jiǎn)單顏色板的實(shí)現(xiàn)
前言
wpf本身沒(méi)有提供顏色板之類的控件,有些業(yè)務(wù)使用場(chǎng)景需要使用顏色板之類的控件,比如設(shè)置彈幕的顏色或設(shè)置文本的顏色等。這里提供一種顏色板的簡(jiǎn)單實(shí)現(xiàn)方法。
一、如何實(shí)現(xiàn)?
1、使用ObjectDataProvider
ObjectDataProvider是wpf中xaml綁定.net任意t類型的媒介,通過(guò)ObjectDataProvider可以直接獲取到System.Windows.Media.Brushes類的屬性列表。System.Windows.Media.Brushes中定義了常見(jiàn)的顏色刷子。
2、定義轉(zhuǎn)換器
由于ObjectDataProvider獲取的Brushes屬性集合是反射集合,并不是直接的Brush對(duì)象,所以需要進(jìn)行數(shù)據(jù)的轉(zhuǎn)換,定義一個(gè)轉(zhuǎn)換器,將屬性(PropertyInfo)轉(zhuǎn)換成Brush對(duì)象。
3、綁定容器
綁定容器的ItemsSource屬性并使用上述的轉(zhuǎn)換器,通過(guò)定義ItemTemplate自定義顏色的顯示控件。
二、使用示例
本示例使用的是ComboBox作為容器顯示顏色板。
1.代碼
xaml代碼如下(示例):
<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="MainWindow" Height="720" Width="1280"> <Window.Resources> <ResourceDictionary> <ObjectDataProvider ObjectInstance="{x:Type Brushes}" MethodName="GetProperties" x:Key="Brushes" /> <local:BrushTypeConverter x:Key="BrushTypeConverter"></local:BrushTypeConverter> </ResourceDictionary> </Window.Resources> <Grid> <ComboBox Margin="0,-200,0,0" Width="240" Height="60" ItemsSource="{Binding Source={StaticResource Brushes}}" SelectedItem="{Binding ForeColor}"> <ComboBox.ItemContainerStyle> <Style TargetType="ComboBoxItem"> <Setter Property="Template" > <Setter.Value> <ControlTemplate TargetType="ComboBoxItem"> <Rectangle Width="210" Height="46" Margin="2,2,2,2" Stroke="#cccccc" StrokeThickness="1" RadiusX="5" RadiusY="5" Fill="{Binding RelativeSource={x:Static RelativeSource.Self},Path=DataContext,Converter={StaticResource BrushTypeConverter}}"></Rectangle> </ControlTemplate> </Setter.Value> </Setter> </Style> </ComboBox.ItemContainerStyle> <ComboBox.Template> <ControlTemplate TargetType="ComboBox"> <Grid> <ToggleButton Cursor="Hand" BorderThickness="0" BorderBrush="Transparent" Panel.ZIndex="1" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"> <ToggleButton.Template> <ControlTemplate> <StackPanel Orientation="Horizontal" Background="Transparent"> <Rectangle Width="210" Height="46" Margin="7,0,5,0" Stroke="#cccccc" StrokeThickness="1" RadiusX="5" RadiusY="5" Fill="{Binding SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}},Converter={StaticResource BrushTypeConverter}}"></Rectangle> <Polyline Grid.Column="1" Points="0,0 5,6 10,0" Margin="0,0,10,0" Width="10" Height="6" Stretch="Fill" Stroke="Black" StrokeThickness="1" /> </StackPanel> </ControlTemplate> </ToggleButton.Template> </ToggleButton> <Popup IsOpen="{TemplateBinding IsDropDownOpen}" Placement="Bottom" x:Name="Popup" Focusable="False" AllowsTransparency="True" PopupAnimation="Slide"> <Border CornerRadius="1" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}" x:Name="DropDown" SnapsToDevicePixels="True"> <ScrollViewer Margin="4,6,4,6" MaxHeight="{TemplateBinding MaxDropDownHeight}" SnapsToDevicePixels="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" CanContentScroll="True"> <!--StackPanel 用于顯示子級(jí),方法是將 IsItemsHost 設(shè)置為 True--> <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" Background="White"/> </ScrollViewer> </Border> </Popup> </Grid> </ControlTemplate> </ComboBox.Template> </ComboBox> </Grid> </Window>
轉(zhuǎn)換器代碼如下(示例):
public class BrushTypeConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) return null; PropertyInfo propertyInfo = value as PropertyInfo; return propertyInfo.GetValue(value) as SolidColorBrush; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return null; } }
2.顯示效果
效果如下(示例):
到此這篇關(guān)于C# wpf簡(jiǎn)單顏色板的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C# wpf 顏色板內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c#編寫webservice服務(wù)引用實(shí)例分享
c#編寫webservice服務(wù)引用實(shí)例分享,大家參考使用吧2013-12-12C#實(shí)現(xiàn)圖形位置組合轉(zhuǎn)換的方法
這篇文章主要介紹了C#實(shí)現(xiàn)圖形位置組合轉(zhuǎn)換的方法,涉及C#使用Matrix操作圖形的相關(guān)技巧,需要的朋友可以參考下2015-06-06深入解析C#編程中struct所定義的結(jié)構(gòu)
這篇文章主要介紹了C#編程中struct所定義的結(jié)構(gòu),與C++一樣,C#語(yǔ)言同時(shí)擁有類和結(jié)構(gòu),需要的朋友可以參考下2016-01-01c#利用Session對(duì)象實(shí)現(xiàn)購(gòu)物車的方法示例
這篇文章主要介紹了c#利用Session對(duì)象實(shí)現(xiàn)購(gòu)物車的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02C#8.0默認(rèn)接口實(shí)現(xiàn)的詳細(xì)實(shí)例
Microsoft使用C#8.0發(fā)布了許多新功能,他們引入的主要功能之一是默認(rèn)接口方法。這篇文章主要給大家介紹了關(guān)于C#8.0默認(rèn)接口實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2021-05-05C# lambda表達(dá)式原理定義及實(shí)例詳解
這篇文章主要介紹了C# lambda表達(dá)式原理定義及實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07