如何使用 DataBInding 解决这个问题?

问题描述 投票:0回答:1

在这个项目中,我想创建一本像素着色书。

<ScrollViewer HorizontalScrollBarVisibility="Auto"
              VerticalScrollBarVisibility="Auto">
    <ItemsControl ItemsSource="{Binding ColorMatrix}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ItemsControl ItemsSource="{Binding}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <pxl:Pixel 
                                Style="{StaticResource Pixel}"
                                PrimaryColor="{Binding Converter={StaticResource ColorConverter}}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ScrollViewer>

ColoringBookView.xaml

这只是 Color[][] 数组的视图。

ColorMatrix 是 Color[][] 类型

public class ColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is System.Drawing.Color drawingColor)
        {
            return Color.FromArgb(drawingColor.A, drawingColor.R, drawingColor.G, drawingColor.B);
        }
        return Colors.Black; 
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

ColoreConverter.cs

public class Pixel : CheckBox
{
    public static readonly DependencyProperty PrimaryColorProperty = DependencyProperty.Register("PrimaryColor", typeof(Color), typeof(Pixel));
    public static readonly DependencyProperty AccentColorProperty = DependencyProperty.Register("AccentColor", typeof(Color), typeof(Pixel));
    public static readonly DependencyProperty IsDrawedProperty = DependencyProperty.Register("IsDrawed", typeof(bool), typeof(Pixel));

    public Color PrimaryColor
    {
        get { return (Color)GetValue(PrimaryColorProperty); }
        set { SetValue(PrimaryColorProperty, value); }
    }
    public Color AccentColor
    {
        get { return (Color)GetValue(AccentColorProperty); }
        set { SetValue(AccentColorProperty, value); }
    }
    public bool IsDrawed
    {
        get { return (bool)GetValue(IsDrawedProperty); }
        set { SetValue(IsDrawedProperty, value); }
    }
}

Pixel.cs

这是 WPF 的自定义控件。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:pxl="clr-namespace:PXL.Core.Theme"
                    xmlns:converters="clr-namespace:PXL.Core.Converters">

    <Style TargetType="{x:Type pxl:Pixel}" x:Key="Pixel">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type pxl:Pixel}">
                    <Grid>
                        <Rectangle Height="30" Width="30">
                            <Rectangle.Fill>
                                <SolidColorBrush Color="{TemplateBinding PrimaryColor}"/>
                            </Rectangle.Fill>
                        </Rectangle>
                        <TextBlock Text="Pixel" Background="{TemplateBinding PrimaryColor}" Foreground="{TemplateBinding PrimaryColor}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

Pixel.xaml

我在数据绑定方面遇到问题,但我不明白出了什么问题。

Pixel.xaml 中的 TextBlock.Background 和 TextBlock.Foreground 不会改变。

wpf data-binding
1个回答
0
投票

Background
Foreground
TextBlock
属性只能设置为
System.Windows.Media.Brush
值。

您无法将它们绑定到

System.Windows.Media.Color
System.Drawing.Color

© www.soinside.com 2019 - 2024. All rights reserved.