首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Elastic WrapPanel和TextTrimming

Elastic WrapPanel和TextTrimming
EN

Stack Overflow用户
提问于 2011-09-29 02:57:46
回答 1查看 865关注 0票数 0

我正在尝试创建一个弹性WrapPanel。这意味着每一列都会伸展到适合面板的宽度,直到新的列适合面板。

列宽基于ItemMinWidth。

我能够创建面板,但我的问题是,如果我放置一个TextBlock并将TextTrimming设置为CharacterEllipsis。在我的面板中,它不会工作。这3个点将不会出现。看起来列的宽度是有效的,但是内容似乎接收到了无穷大的宽度。

这是我的面板:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace WrapPanelTest
{
    public class ElasticWrapPanel : Panel
    {
        #region Constructor

        public ElasticWrapPanel()
        {
        }

        #endregion

        #region Properties

        [TypeConverter(typeof(LengthConverter))]
        public double ItemMinWidth
        {
            get
            {
                return (double)GetValue(ItemMinWidthProperty);
            }
            set
            {
                SetValue(ItemMinWidthProperty, value);
            }
        }

        public static readonly DependencyProperty ItemMinWidthProperty = DependencyProperty.Register("ItemMinWidth", typeof(double), typeof(ElasticWrapPanel));

        [TypeConverter(typeof(LengthConverter))]
        public double ColumnWidth
        {
            get
            {
                return (double)GetValue(ColumnWidthProperty);
            }
            private set
            {
                SetValue(ColumnWidthProperty, value);
            }
        }

        public static readonly DependencyProperty ColumnWidthProperty = DependencyProperty.Register("ColumnWidth", typeof(double), typeof(ElasticWrapPanel));

        #endregion

        #region Protected methods

        protected override Size MeasureOverride(Size availableSize)
        {
            _uiElementsRect.Clear();
            _columnCount = CalculateColumnCount(availableSize);

            if (_columnCount > 0)
            {
                ColumnWidth = (int)Math.Floor(availableSize.Width / _columnCount);
                double rowHeight = 0;
                double lastRowHeight = 0;
                int currentColumn = 0;
                int currentRow = 0;

                foreach (UIElement children in InternalChildren)
                {
                    children.Measure(availableSize);

                    rowHeight = Math.Max(rowHeight, children.DesiredSize.Height);

                    Rect childrenRect = new Rect(0, 0, 0, 0);
                    childrenRect.X = currentColumn * ColumnWidth;
                    childrenRect.Y = lastRowHeight;
                    childrenRect.Width = ColumnWidth;
                    childrenRect.Height = children.DesiredSize.Height;

                    _uiElementsRect.Add(children, childrenRect);

                    currentColumn++;

                    if (currentColumn == _columnCount)
                    {
                        lastRowHeight += rowHeight;
                        currentRow++;
                        rowHeight = 0;
                        currentColumn = 0;
                    }
                }
            }

            if (_uiElementsRect.Any())
            {
                double maxBottom = _uiElementsRect.Max(ui => ui.Value.Bottom);

                return new Size(availableSize.Width, maxBottom);
            }
            else
                return base.MeasureOverride(availableSize);
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            if (_columnCount > 0)
                foreach (KeyValuePair<UIElement, Rect> keyValuePair in _uiElementsRect)
                    keyValuePair.Key.Arrange(keyValuePair.Value);

            return base.ArrangeOverride(finalSize);
        }

        #endregion

        #region Private methods

        private int CalculateColumnCount(Size availableSize)
        {
            if (ItemMinWidth == 0 || Double.IsNaN(ItemMinWidth) || Double.IsInfinity(availableSize.Width))
                return 0;
            else
                return (int)Math.Floor(availableSize.Width / ItemMinWidth);
        }

        #endregion

        #region Private members

        int _columnCount = 0;

        IDictionary<UIElement, Rect> _uiElementsRect = new Dictionary<UIElement, Rect>();

        #endregion
    }
}

这里有一个例子,与TextTrimming工作的WrapPanel进行比较:

代码语言:javascript
复制
<Window x:Class="WrapPanelTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WrapPanelTest"
        Title="MainWindow" 
        Height="480" 
        Width="640">

    <Window.Resources>

        <Style x:Key="TextBlockStyle" TargetType="{x:Type TextBlock}">
            <Setter Property="Text" Value="Lorem ipsum dolor sit amet, consectetur adipiscing elit."/>
            <Setter Property="TextTrimming" Value="CharacterEllipsis"/>
            <Setter Property="Margin" Value="1"/>
            <Setter Property="Background" Value="Silver"/>
        </Style>

    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <WrapPanel ItemWidth="200">
            <TextBlock Style="{StaticResource TextBlockStyle}"/>
            <TextBlock Style="{StaticResource TextBlockStyle}"/>
            <TextBlock Style="{StaticResource TextBlockStyle}"/>
            <TextBlock Style="{StaticResource TextBlockStyle}"/>
            <TextBlock Style="{StaticResource TextBlockStyle}"/>
            <TextBlock Style="{StaticResource TextBlockStyle}"/>
            <TextBlock Style="{StaticResource TextBlockStyle}"/>
            <TextBlock Style="{StaticResource TextBlockStyle}"/>
            <TextBlock Style="{StaticResource TextBlockStyle}"/>
        </WrapPanel>

        <local:ElasticWrapPanel ItemMinWidth="200" Grid.Row="1">
            <TextBlock Style="{StaticResource TextBlockStyle}"/>
            <TextBlock Style="{StaticResource TextBlockStyle}"/>
            <TextBlock Style="{StaticResource TextBlockStyle}"/>
            <TextBlock Style="{StaticResource TextBlockStyle}"/>
            <TextBlock Style="{StaticResource TextBlockStyle}"/>
            <TextBlock Style="{StaticResource TextBlockStyle}"/>
            <TextBlock Style="{StaticResource TextBlockStyle}"/>
            <TextBlock Style="{StaticResource TextBlockStyle}"/>
            <TextBlock Style="{StaticResource TextBlockStyle}"/>
        </local:ElasticWrapPanel>

    </Grid>
</Window>

您应该看到,在WrapPanel中,3个点出现在每个元素的末尾,但没有出现在我的面板中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-29 03:12:35

你的TextBlocks没有尺寸设置,所以你可以随意伸展。

另一种方法是,考虑在MeasureOverride方法中限制它们的大小

代码语言:javascript
复制
foreach (UIElement children in InternalChildren)
{
    children.Measure(availableSize);

    ((FrameworkElement)children).MaxWidth = ColumnWidth;

    // Other code
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7588136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档