您好,登錄后才能下訂單哦!
C#怎么實現wpf簡單顏色板,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
wpf本身沒有提供顏色板之類的控件,有些業務使用場景需要使用顏色板之類的控件,比如設置彈幕的顏色或設置文本的顏色等。這里提供一種顏色板的簡單實現方法。
ObjectDataProvider是wpf中xaml綁定.net任意t類型的媒介,通過ObjectDataProvider可以直接獲取到System.Windows.Media.Brushes類的屬性列表。System.Windows.Media.Brushes中定義了常見的顏色刷子。
由于ObjectDataProvider獲取的Brushes屬性集合是反射集合,并不是直接的Brush對象,所以需要進行數據的轉換,定義一個轉換器,將屬性(PropertyInfo)轉換成Brush對象。
綁定容器的ItemsSource屬性并使用上述的轉換器,通過定義ItemTemplate自定義顏色的顯示控件。
本示例使用的是ComboBox作為容器顯示顏色板。
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 用于顯示子級,方法是將 IsItemsHost 設置為 True--> <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" Background="White"/> </ScrollViewer> </Border> </Popup> </Grid> </ControlTemplate> </ComboBox.Template> </ComboBox> </Grid> </Window>
轉換器代碼如下(示例):
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; } }
效果如下(示例):
看完上述內容,你們掌握C#怎么實現wpf簡單顏色板的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。