代码之家  ›  专栏  ›  技术社区  ›  Keven M

将WPF扩展工具包ColorPicker SelectedColor属性绑定到DependencyProperty

  •  1
  • Keven M  · 技术社区  · 6 年前

    我想把 SelectedColor wpf扩展工具箱的属性colorPicker到 DependencyProperty 带类型 SolidColorBrush . 然后,该颜色应该用作画布上/中形状的填充颜色。

    这不起作用,据我所知,问题是 选择颜色 ColorPicker的属性实际上是一个具有类型的对象 Color? . 我的问题是,我如何才能让这个工作?如果我只是通过一个连接到事件的方法在代码中完成它,我可以使用 SelectedColor.Value ,但afaik在XAML中不是选项。

    我确实尝试使用常规属性并让类继承自 INotifyPropertyChanged (如 SelectedColor binding doesn't update from ColorPicker to Model )而不是来自 DependencyObject 但这也不管用。

    我更喜欢用绑定来实现这一点,因为我正试图在使用MVVM的实践中获得更多。我最近才知道如何使用MVVM,实际上我现在更喜欢它,所以我真的希望避免仅仅使用代码隐藏。

    XAML:

        <toolkit:ColorPicker x:Name="fillColor" Grid.Column="1" Grid.Row="8" Margin="5" VerticalAlignment="Top" SelectedColor="{Binding Path=FillColor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedColorChanged="fillColor_SelectedColorChanged" />
        <toolkit:ColorPicker x:Name="lineColor" Grid.Column="2" Grid.Row="8" Margin="5" VerticalAlignment="Top" />
    

    C: private static dependencyProperty fillColorProperty=dependencyProperty.register(“fillColor”,typeof(solidColorBrush)), 类型(TuckboxModel)、新框架工作特性元数据(Brushes.purple));

        private static DependencyProperty LineColorProperty = DependencyProperty.Register("LineColor", typeof(SolidColorBrush),
            typeof(TuckboxModel), new FrameworkPropertyMetadata(Brushes.Black));
    
    public SolidColorBrush FillColor
        {
            get { return (SolidColorBrush)GetValue(FillColorProperty); }
            set
            {
                SetValue(FillColorProperty, value);
            }
        }
    
        public SolidColorBrush LineColor
        {
            get { return (SolidColorBrush)GetValue(LineColorProperty); }
            set { SetValue(LineColorProperty, value); }
        }
    
    public void CreateBox(Canvas drawingCanvas, TuckboxModel model, double width, double height, double depth, SolidColorBrush shading, SolidColorBrush outline)
        {
            double xOrigin = 50;
            double yOrigin = 450;
    
            double xCoord = xOrigin;
            double yCoord = yOrigin;
            Shape item;
            double flapSize;
            long count;
    
            //Remove all previous shapes from Canvas
            drawingCanvas.Children.Clear();
    
            Path border = new Path();
            border.Stroke = model.LineColor;
            border.StrokeThickness = 1.0;            
            border.Fill = model.FillColor;
            //border.Fill = shading;
    
            yCoord -= ToPts(depth);
            xCoord += ToPts(depth);
    
            RectangleGeometry rectangleA = new RectangleGeometry();
            Rect rectA = new Rect(xCoord, yCoord, ToPts(width), ToPts(depth));
            rectangleA.Rect = rectA;
    
            xCoord += ToPts(width);
    
            RectangleGeometry rectangleB = new RectangleGeometry();
            Rect rectB = new Rect(xCoord + 3, yCoord, ToPts(depth) - 5, ToPts(depth));
            rectangleB.Rect = rectB;
    
            xCoord += ToPts(depth);
    
            RectangleGeometry rectangleC = new RectangleGeometry();
            Rect rectC = new Rect(xCoord + 2, yCoord, ToPts(width) - 4, ToPts(depth) - 2);
            rectangleC.Rect = rectC;
    
            xCoord += ToPts(width);
    
            RectangleGeometry rectangleD = new RectangleGeometry();
            Rect rectD = new Rect(xCoord + 2, yCoord, ToPts(depth) - 5, ToPts(depth));
            rectangleD.Rect = rectD;
    
            xCoord = xOrigin;
            yCoord -= ToPts(height);
    
            RectangleGeometry rectangleE = new RectangleGeometry();
            Rect rectE = new Rect(xCoord, yCoord, ToPts(depth), ToPts(height));
            rectangleE.Rect = rectE;
    
            xCoord += ToPts(depth);
    
            RectangleGeometry rectangleF = new RectangleGeometry();
            Rect rectF = new Rect(xCoord, yCoord, ToPts(width), ToPts(height));
            rectangleF.Rect = rectF;
    
            xCoord += ToPts(width);
    
            RectangleGeometry rectangleG = new RectangleGeometry();
            Rect rectG = new Rect(xCoord, yCoord, ToPts(depth), ToPts(height));
            rectangleG.Rect = rectG;
    
            xCoord += ToPts(depth);
    
            RectangleGeometry rectangleH = new RectangleGeometry();
            Rect rectH = new Rect(xCoord, yCoord, ToPts(width), ToPts(height));
            rectangleH.Rect = rectH;
    
            xCoord += ToPts(width);
    
            RectangleGeometry rectangleI = new RectangleGeometry();
            Rect rectI = new Rect(xCoord, yCoord, ToPts(depth) - 3, ToPts(height));
            rectangleI.Rect = rectI;
    
            xCoord = xOrigin;
            yCoord -= ToPts(depth);
    
            RectangleGeometry rectangleJ = new RectangleGeometry();
            Rect rectJ = new Rect(xCoord + 1, yCoord, ToPts(depth) - 2, ToPts(depth));
            rectangleJ.Rect = rectJ;
    
            xCoord += ToPts(depth);
    
            RectangleGeometry rectangleK = new RectangleGeometry();
            Rect rectK = new Rect(xCoord, yCoord, ToPts(width), ToPts(depth));
            rectangleK.Rect = rectK;
    
            xCoord += ToPts(width);
    
            RectangleGeometry rectangleL = new RectangleGeometry();
            Rect rectL = new Rect(xCoord + 1, yCoord, ToPts(depth) - 2, ToPts(depth));
            rectangleL.Rect = rectL;            
    
            EllipseGeometry ellipseA = new EllipseGeometry();
            ellipseA.Center = new Point(rectK.Right - (rectK.Width / 2), yCoord);
            ellipseA.RadiusX = rectK.Width / 2;
    
            if(height < 1.0)
                flapSize = ToPts(height);
            else
                flapSize = 72;
    
            ellipseA.RadiusY = flapSize;
    
            GeometryGroup finalShape = new GeometryGroup();
            finalShape.Children.Add(rectangleA);
            finalShape.Children.Add(rectangleB);
            finalShape.Children.Add(rectangleC);
            finalShape.Children.Add(rectangleD);
            finalShape.Children.Add(rectangleE);
            finalShape.Children.Add(rectangleF);
            finalShape.Children.Add(rectangleG);
    
            finalShape.Children.Add(rectangleH);
            finalShape.Children.Add(rectangleI);
            finalShape.Children.Add(rectangleJ);
            finalShape.Children.Add(rectangleK);
            finalShape.Children.Add(rectangleL);
            finalShape.Children.Add(ellipseA);
    
            finalShape.FillRule = FillRule.Nonzero;
    
            border.Data = finalShape;
    
            drawingCanvas.Children.Add(border);
        }
    

    任何帮助都将不胜感激!

    1 回复  |  直到 6 年前
        1
  •  0
  •   XAMlMAX    6 年前

    我想A Converter 是你麻烦的答案。下面是转换器类的示例:

    using System;
    using System.Globalization;
    using System.Windows.Data;
    
    namespace SO_app.Converters
    {
        public class DebugConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (null == value) {
                    return null;
                }
                // For a more sophisticated converter, check also the targetType and react accordingly..
                if (value is Color) {
                    Color color = (Color)value;
                    return new SolidColorBrush(color);
                }
                // You can support here more source types if you wish
                // For the example I throw an exception
    
                Type type = value.GetType();
                throw new InvalidOperationException("Unsupported type ["+type.Name+"]");  
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return value;
            }
        }
    }  
    

    然后在您的XAML中,它将如下所示:
    这是我的项目:

    xmlns:converter="clr-namespace:SO_app.Converters"
    

    然后在参考资料中:

    <converter:DebugConverter x:Key="DebugConverter"/>
    

    然后在XAML:

    <toolkit:ColorPicker x:Name="fillColor" Grid.Column="1" Grid.Row="8" Margin="5" VerticalAlignment="Top" SelectedColor="{Binding Path=FillColor, Converter={StaticResource DebugConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedColorChanged="fillColor_SelectedColorChanged" />  
    

    注:
    类型转换的代码来自 SO post .
    更新:
    检查 this SO post 这意味着工具箱中有一个内置的转换器