1 新建wpf项目
2 新建wpf 用户控件库
3 新建MyTextUC,为其添加两个依赖属性,Text和Color,注意前端设置属性是string类型,wpf自带的转换器只能将string转换成color,但无法转换成Brush,所以需要添加ColorToBrush转换器
<UserControl x:Class="CustomControls.MyTextUC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CustomControls"
mc:Ignorable="d" x:Name="myTextUC" Height="204" Width="475">
<UserControl.Resources>
<ResourceDictionary>
<local:ColorToBrushConverter x:Key="ColorToBrushConverter" />
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<TextBlock Background="{Binding ElementName=myTextUC, Path=Color,Converter={StaticResource ColorToBrushConverter}}" Text="{Binding ElementName=myTextUC, Path=Text}"/>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CustomControls
{
/// <summary>
/// ColorPickerUC.xaml 的交互逻辑
/// </summary>
public partial class MyTextUC : UserControl
{
public MyTextUC()
{
InitializeComponent();
}
public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(MyTextUC),
new PropertyMetadata(Colors.Black));
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(MyTextUC), new PropertyMetadata(""));
public Color Color
{
get { return (Color)GetValue(ColorProperty); }
set { SetValue(ColorProperty, value); }
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using Color = System.Windows.Media.Color;
namespace CustomControls
{
public class ColorToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Color color = (Color)value;
return new SolidColorBrush(color);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
4 在MainWindow前端引入自定义类库,修改代码
<Window x:Class="CustomControlDemo.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:local="clr-namespace:CustomControlDemo"
mc:Ignorable="d"
xmlns:lib="clr-namespace:CustomControls;assembly=CustomControls"
Title="MainWindow" Height="450" Width="800">
<Grid>
<lib:MyTextUC Text="hello world" Color="Blue"/>
</Grid>
</Window>