Spacing and aligning Rectangles in ItemsControl

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-25 08:22:49

问题


I've this class:

public class MyRect : FrameworkElement
{
    public Visual Visual { get; set; }
    protected override int VisualChildrenCount => 1;
    protected override Visual GetVisualChild(int index) => Visual;
    protected override void OnRender(DrawingContext drawingContext)
    {
        var drawing = new DrawingVisual();
        using (var dc = drawing.RenderOpen())
        {
            var brush = new SolidColorBrush(Colors.Green);
            var pen = new Pen(new SolidColorBrush(Colors.Blue), 1);
            var rect = new Rect(new Size(Width, Height));
            dc.DrawRectangle(brush, pen, rect);
        }
        Visual = drawing;
    }
}

for drawing Rectangles. On a button click, a new Rectangle is added to an ObservableCollection named RectCollection:

RectCollection.Insert(0, new MyRect() {Width = 20, Height = rand.NextDouble() * 100 }); 

and RectCollection is the ItemSource of an ItemsControl:

<ItemsControl ItemsSource="{Binding RectCollection}" VerticalAlignment="Bottom" VerticalContentAlignment="Bottom">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

It draws rectangle BUT there's no space between them. I've tried setting margin in DataTemplate like this:

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <StackPanel Width="35" Margin="5 0 5 0"/>
    </DataTemplate>
</ItemsControl.ItemTemplate> 

that doesn't work! Another problem is that the VerticalContentAlignment="Bottom" doesn't align Rectangles bottom to the baseline.


EDIT

public class MyRect : FrameworkElement
{
    public Visual Visual { get; set; }
    protected override int VisualChildrenCount => 1;
    protected override Visual GetVisualChild(int index) => Visual;
    protected override void OnRender(DrawingContext drawingContext)
    {
        var drawing = new DrawingVisual();
        using (drawingContext = drawing.RenderOpen())
        {
            var grid = new Grid();
            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(0, GridUnitType.Star) });
            grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });

            var text = new TextBlock()
            {
                Text = Height.ToString("N2"),
                Foreground = new SolidColorBrush(Colors.Black),
                LayoutTransform = new RotateTransform(-90),
                VerticalAlignment = VerticalAlignment.Top,
                Margin = new Thickness(0, 0, 0, 5)
            };
            var border = new Border() 
            { 
                Width = Width, 
                Height = Height, 
                Background = new SolidColorBrush(Colors.Green), 
                CornerRadius = new CornerRadius(5, 5, 0, 0) 
            };

            grid.Children.Add(text);
            grid.Children.Add(border);
            Grid.SetRow(border, 1);

            grid.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
            drawingContext.DrawRectangle(new VisualBrush(grid), null, new Rect(grid.DesiredSize));
        }
        Visual = drawing;
    }
}

回答1:


Here is an example for a simple bar chart, using just some basic elements in the ItemTemplate of an ItemsControl:

<ItemsControl ItemsSource="{Binding}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid Width="20" Height="100" Margin="2">
                <Rectangle VerticalAlignment="Bottom"
                           Height="{Binding}"
                           Fill="LightGray"/>
                <TextBlock Text="{Binding StringFormat={}{0:N2}}">
                    <TextBlock.LayoutTransform>
                        <RotateTransform Angle="-90"/>
                    </TextBlock.LayoutTransform>
                </TextBlock>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Its DataContext is set to a collection of double values, e.g. like:

Random r = new Random();

DataContext = Enumerable.Range(0, 20).Select(i => r.NextDouble() * 100);


来源:https://stackoverflow.com/questions/59148608/spacing-and-aligning-rectangles-in-itemscontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!