Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize wpf simple Color Pad with C #

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

C # how to achieve wpf simple color board, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Preface

Wpf itself does not provide controls such as color palettes, and some business scenarios need to use controls such as color palettes, such as setting the color of on-screen comments or setting the color of text. A simple implementation of color swatches is provided here.

First, how to achieve it?

1. Use ObjectDataProvider

ObjectDataProvider is the medium of xaml binding .net arbitrary t type in wpf, and the property list of System.Windows.Media.Brushes class can be obtained directly through ObjectDataProvider. Common color brushes are defined in System.Windows.Media.Brushes.

2. Define the converter

Because the collection of Brushes attributes obtained by ObjectDataProvider is a reflection collection, not a direct Brush object, it is necessary to convert the data and define a converter to convert the PropertyInfo into a Brush object.

3. Bind the container

Bind the ItemsSource property of the container and use the converter above to customize the display control of the color by defining the ItemTemplate.

Second, use examples

This example uses ComboBox as a container to display the color swatch.

1. Code

The xaml code is as follows (example):

The converter code is as follows (example):

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. Display effect

The effect is as follows (example):

After reading the above, have you mastered how C # implements the wpf simple color palette? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report