水平ItemsControl中的常量项宽度

时间:2011-09-30 03:30:18

标签: wpf grid itemscontrol stackpanel

我需要一个横向组织的ItemsControl,它将所有项目限制为相同的宽度。我正在使用的项目是UserControl,并构建一个自动调整大小的TextBlock,显示一个int值(包含在依赖项属性中),周围有Border。问题是较小的值会导致较窄的项目,并且我需要所有项目都相同。我考虑了一些解决方案,但我似乎无法做任何工作。

第一种方法是将ItemsPanel模板设置为Grid。这样我可以使用代码隐藏生成基于数据源所需的列数,并将所有列宽设置为*。谜题是弄清楚如何为每个项目设置网格列附加属性。

第二种解决方案是将ItemsPanel模板设置为StackPanel。这会自动正确排列项目,但我无法将每个项目的宽度设置为最宽项目的宽度。

最后一个解决方案是将ItemsPanel模板设置为带有代码隐藏的UniformGrid,以便在数据源发生更改时设置列数。这将自动正确地排列物品并提供均匀的宽度。我的问题在于我根本无法显示任何项目。我试过手动添加按钮,它们显示很好。我的UserControl不会出现。我在下面列出了这个解决方案。

    <Window x:Class="Learning_WPF.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:my="clr-namespace:Learning_WPF"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            Title="DateTape" Height="176" Width="500">
        <Window.Resources>
            <my:DateList x:Key="dateList" CollectionChanged="DateList_CollectionChanged" />
        </Window.Resources>
        <ItemsControl x:Name="itemsControl1" ItemsSource="{Binding Source={StaticResource dateList}, Path=/}" Grid.Row="1">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid x:Name="daysGrid" Rows="1" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </Window>

    public partial class MainWindow : Window
    {
        UniformGrid daysGrid;
        DateList dateList;

        public MainWindow()
        {
            InitializeComponent();
            daysGrid = (UniformGrid)itemsControl1.ItemsPanel.LoadContent();
            dateList = (DateList)FindResource("dateList");
            dateList.Fill(DateTime.Today, DateTime.Today.AddDays(10));
        }

        private void DateList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            daysGrid.Columns = dateList.Count;
        }

    }

    public class DateList : ObservableCollection<Date>
    {
        public void Fill(DateTime first, DateTime last)
        {
            // implementation fills the array with all of the days between first and last, inclusively
        }
    }

也许有更好的方法来实现我想要实现的目标(可能比控件更具图形性)......

2 个答案:

答案 0 :(得分:1)

如果UniformGrid不会为您的解决方案削减它,您可以尝试为ItemContainerStyle设置ItemsControl(假设您可以使用{{1}与ListBox个孩子一起),像这样:

ListBoxItem

答案 1 :(得分:0)

我终于使用Grid让它工作了。我不得不做一些重要的代码隐藏,由于三重布局,界面明显缓慢,但它的工作原理。欢迎提出改善建议。将此控件插入窗口时,请在StartDate属性之前将EndDate属性更改为其默认值;否则VS会锁定。

XAML

    <UserControl x:Class="MyProject.DateTape"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 xmlns:my="clr-namespace:MyProject"
                 xmlns:sys="clr-namespace:System;assembly=mscorlib"
                 mc:Ignorable="d" 
                 d:DesignHeight="45" d:DesignWidth="188">
        <UserControl.Resources>
            <Thickness x:Key="bottomThickness" Bottom="1" Top="1" Left="0.5" Right="0.5" />
            <Thickness x:Key="topThickness" Bottom="0" Top="1" Left="0.5" Right="0.5" />
            <Style TargetType="Border">
                <Setter Property="BorderBrush" Value="#FFBEBEBE"/>
            </Style>
            <Style TargetType="TextBlock">
                <Setter Property="TextAlignment" Value="Center"/>
                <Setter Property="Margin" Value="6,0"/>
            </Style>
        </UserControl.Resources>
        <Grid x:Name="mainGrid"/>
    </UserControl>

C#

        public partial class DateTape : UserControl
        {
            public static readonly DependencyProperty StartDateProperty =
                DependencyProperty.Register("StartDate", typeof(DateTime), typeof(DateTape),
                                            new PropertyMetadata(new PropertyChangedCallback(OnDatesChanged)));
            public static readonly DependencyProperty EndDateProperty =
                DependencyProperty.Register("EndDate", typeof(DateTime), typeof(DateTape),
                                            new PropertyMetadata(new PropertyChangedCallback(OnDatesChanged)));

            [Category("Common"),
             TypeConverter(typeof(DateConverter))]
            public DateTime StartDate
            {
                get { return (DateTime)GetValue(StartDateProperty); }
                set { SetValue(StartDateProperty, value); }
            }
            [Category("Common"),
             TypeConverter(typeof(DateConverter))]
            public DateTime EndDate
            {
                get { return (DateTime)GetValue(EndDateProperty); }
                set { SetValue(EndDateProperty, value); }
            }

            public DateTape()
            {
                InitializeComponent();
            }

            private void Layout()
            {
                int dayCount = (EndDate - StartDate).Days;
                double max = 0;
                DateTime current = StartDate;
                ColumnDefinition columnDefinition;
                Border border;
                TextBlock textBlock;
                Binding binding;
                Size infinity = new Size(double.PositiveInfinity, double.PositiveInfinity);
                Thickness bottomThickness = (Thickness)Resources["bottomThickness"],
                          topThickness = (Thickness)Resources["topThickness"];
                mainGrid.ColumnDefinitions.Clear();
                for (int i = 0; i <= dayCount; i++, current += TimeSpan.FromDays(1))
                {
                    mainGrid.ColumnDefinitions.Add(columnDefinition = new ColumnDefinition());
                    // Add a day
                    border = new Border();
                    textBlock = new TextBlock();
                    textBlock.Text = current.Day.ToString();
                    border.Child = textBlock;
                    binding = new Binding();
                    binding.Source = bottomThickness;
                    border.SetBinding(Border.BorderThicknessProperty, binding);
                    mainGrid.Children.Add(border);
                    border.Measure(infinity);
                    max = Math.Max(max, border.DesiredSize.Width);
                    Grid.SetRow(border, 2);
                    Grid.SetColumn(border, i);
                }
                foreach (ColumnDefinition cd in mainGrid.ColumnDefinitions)
                {
                    cd.MinWidth = max;
                }
            }

            private static void OnDatesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                (d as DateTape).Layout();
            }

编辑:

我找到了一种方法,在不使用多个布局过程的情况下,通过查询每个边框项的DesiredSize,我将它们添加到网格并跟踪最大宽度。