В этом проекте я хочу создать пиксельную раскраску.
<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
Это CustomControl для 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
У меня проблемы с привязкой данных, но я не понимаю, в чем дело.
TextBlock.Background и TextBlock.Foreground в Pixel.xaml не изменяются.
Атрибуты цвета TextBlock не изменятся, поскольку нет ничего, что могло бы вызвать их изменение. Ожидаете ли вы, что они будут динамично меняться? Ваш ColorConverter всегда возвращает черный цвет (я предполагаю, что вы не используете System.Drawing.Color в своем коде — не следует).





Свойствам Background и ForegroundTextBlock можно установить только значения System.Windows.Media.Brush.
Вы не можете привязать их к System.Windows.Media.Color или System.Drawing.Color.
Я добавил конвертер из System.Windows.Media.Color в System.Windows.Media.Brush, но это ничего не меняет. Прямоугольник всегда прозрачный.
Конвертер? Вам следует изменить тип ваших свойств.
Вы должны обернуть
ItemsPresenterControlTemplateвScrollViewer, а не в полныйItemsControl(это приводит к другому поведению прокрутки — вы хотите прокручивать содержимое или элементы элемента управления, а не сам элемент управления). Вместо этого я рекомендую использоватьListBox: это расширенныйItemsControl, который включает в себя средство просмотра прокрутки и другие улучшения производительности. Из соображений производительности вам также не следует привязываться к массиву. Вместо этого привяжитеObservableCollection.