我需要填写可点击的按钮,基于细胞的面板,看起来像这样:如何填写ItemsControl中的按钮
这与矩形做:
<ItemsControl ItemsSource="{Binding Path=Cells}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle>
<Rectangle.Fill>
<SolidColorBrush Color="Red"></SolidColorBrush>
</Rectangle.Fill>
</Rectangle>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<commonControls:UniformGrid ElementsGap="5" HorizontalCount="{Binding Path=CellsInRow}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
,但我需要用按钮来完成。我想的是:
<ItemsControl ItemsSource="{Binding Path=Cells}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button>
<Button.Background>
<SolidColorBrush Color="Red"></SolidColorBrush>
</Button.Background>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<commonControls:UniformGrid ElementsGap="5" HorizontalCount="{Binding Path=CellsInRow}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
但似乎这样的:
如何我可以从按键的第一图像做细胞?下面UniformGrid的
代码:
public class UniformGrid : Panel
{
public static readonly DependencyProperty HorizontalCountProperty =
DependencyProperty.Register("HorizontalCount", typeof (int), typeof (UniformGrid),
new PropertyMetadata(default(int)));
public int HorizontalCount
{
get { return (int) GetValue(HorizontalCountProperty); }
set { SetValue(HorizontalCountProperty, value); }
}
public static readonly DependencyProperty ElementsGapProperty =
DependencyProperty.Register("ElementsGap", typeof (double), typeof (UniformGrid),
new PropertyMetadata(default(double)));
public double ElementsGap
{
get { return (double) GetValue(ElementsGapProperty); }
set { SetValue(ElementsGapProperty, value); }
}
protected override Size MeasureOverride(Size availableSize)
{
return new Size();
}
protected override Size ArrangeOverride(Size finalSize)
{
if (Children != null && Children.Count != 0)
{
var squareSideForElement = (finalSize.Width - (HorizontalCount - 1)*ElementsGap)/HorizontalCount;
var sizeOfElement = new Size(squareSideForElement, squareSideForElement);
for (var i = 0; i < Children.Count; i++)
{
var rowIndex = i%HorizontalCount;
var columnIndex = i/HorizontalCount;
var resultPoint = new Point
{
X = rowIndex*(squareSideForElement + ElementsGap),
Y = columnIndex*(squareSideForElement + ElementsGap)
};
Children[i].Arrange(new Rect(resultPoint, sizeOfElement));
}
}
return finalSize;
}
}
你知道已经有一个[UniformGrid(http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.uniformgrid_properties.aspx)在WPF? – Clemens