我不确定最好的方法是什么,所以我最好问你。
我需要实现这样的图形:
每个字段对应一个其他变量,并应相应地着色。
最简单的方式(不是最快):
在XAML中单独创建每个对象,并使用绑定或处理程序
进行<Rectangle Name="rect0_0" Fill="White" Stroke="Black" StrokeThickness="1" RadiusX="1" RadiusY="10" Margin="12,250,354,12" />
或类似于:
void InitializeRectangle()
{
aRectangle = new RectangleGeometry[MAX_CHANNELS];
for (int i = 0; i < aRectangle.Length; i++)
{
aRectangle[i] = new RectangleGeometry(new Rect(recX, recY, recWidth,recHeight ));
GeometryGroup1.Children.Add(aRectangle[i]);
recX += recGab;
}
}
void PaintRectangle()
{
myPath1.Data = GeometryGroup1;
myPath1.Stroke = Brushes.Black;
myPath1.StrokeThickness = 1;
mySolidColorBrush.Color = Color.FromArgb(255, 204, 204, 255);
myPath1.Fill = mySolidColorBrush;
Canvas myCanvas = new Canvas();
myCanvas.Children.Add(myPath1);
this.Content = myCanvas;
}
但我不能用这种方法分别给它们着色..
如何为每个矩形着色并在GUI中查看?
答案 0 :(得分:2)
快速示例,说明如何实现目标。
XAML:
<Window x:Class="WpfTestBench.GraphicSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfTestBench="clr-namespace:WpfTestBench"
Title="Graphic sample" Height="130" Width="570">
<!-- Outer items control to hold rows -->
<ItemsControl ItemsSource="{Binding Rows}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="wpfTestBench:Row">
<StackPanel Orientation="Horizontal">
<!-- Row identifier -->
<Label Content="{Binding Id}" />
<!-- Inner items control with rectangles -->
<ItemsControl ItemsSource="{Binding Squares}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="wpfTestBench:Square">
<Rectangle Stroke="Black" StrokeThickness="1"
Width="30" Height="20" Margin="2, 0"
Fill="{Binding Value, Converter={wpfTestBench:IntToColorConverter}}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Window>
代码隐藏:
using System;
using System.Collections.Generic;
namespace WpfTestBench
{
public partial class GraphicSample
{
public GraphicSample()
{
InitializeComponent();
DataContext = new GraphicContext();
}
}
public class GraphicContext
{
private readonly Random _random = new Random();
public GraphicContext()
{
Rows = new List<Row>();
for (var i = 1; i <= 4; i++)
Rows.Add(new Row(_random, i));
}
public IList<Row> Rows { get; set; }
}
public class Row
{
private const int Size = 16;
public Row(Random random, int id)
{
Id = id;
Squares = new List<Square>();
for (var i = 0; i < Size; i++)
Squares.Add(new Square(random.Next(20)));
}
public int Id { get; private set; }
public IList<Square> Squares { get; private set; }
}
public class Square
{
public Square(int value)
{
Value = value;
}
public int Value { get; set; }
}
}
转换器:
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;
namespace WpfTestBench
{
public class IntToColorConverter : MarkupExtension, IValueConverter
{
private static IntToColorConverter _instance;
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var convertedValue = (int)value;
if (convertedValue < 5)
return new SolidColorBrush(Colors.White);
if (convertedValue < 10)
return new SolidColorBrush(Colors.Green);
if (convertedValue < 15)
return new SolidColorBrush(Colors.Yellow);
return new SolidColorBrush(Colors.Red);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
#endregion
public override object ProvideValue(IServiceProvider serviceProvider)
{
return _instance ?? (_instance = new IntToColorConverter());
}
}
}
执行结果: