Wpf附加属性绑定不设置属性

时间:2015-03-30 00:49:42

标签: c# wpf binding

我正在使用此项目的范围面板http://www.codeproject.com/Articles/30881/Creating-an-Outlook-Calendar-Using-WPF-Part

using System;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;

namespace Application
{
    public class RangePanel : Panel
    {
        public static DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(double), typeof(RangePanel), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsArrange));
        public static DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(RangePanel), new FrameworkPropertyMetadata(100.0, FrameworkPropertyMetadataOptions.AffectsArrange));
        public static DependencyProperty OrientationProperty = DependencyProperty.Register("Orientation", typeof(Orientation), typeof(RangePanel), new FrameworkPropertyMetadata(Orientation.Vertical, FrameworkPropertyMetadataOptions.AffectsArrange));
        public static DependencyProperty BeginProperty = DependencyProperty.RegisterAttached("Begin", typeof(double), typeof(UIElement), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsArrange));
        public static DependencyProperty EndProperty = DependencyProperty.RegisterAttached("End", typeof(double), typeof(UIElement), new FrameworkPropertyMetadata(100.0, FrameworkPropertyMetadataOptions.AffectsArrange));

        public static void SetBegin(UIElement element, double value)
        {
            element.SetValue(BeginProperty, value);
        }

        public static double GetBegin(UIElement element)
        {
            return (double)element.GetValue(BeginProperty);
        }

        public static void SetEnd(UIElement element, double value)
        {
            element.SetValue(EndProperty, value);
        }

        public static double GetEnd(UIElement element)
        {
            return (double)element.GetValue(EndProperty);
        }

        public double Maximum
        {
            get { return (double)GetValue(MaximumProperty); }
            set { SetValue(MaximumProperty, value); }
        }

        public double Minimum
        {
            get { return (double)GetValue(MinimumProperty); }
            set { SetValue(MinimumProperty, value); }
        }

        public Orientation Orientation
        {
            get { return (Orientation)GetValue(OrientationProperty); }
            set { SetValue(OrientationProperty, value); }
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            double containerRange = (Maximum - Minimum);

            foreach (UIElement element in Children)
            {
                double begin = (double)element.GetValue(BeginProperty);
                double end = (double)element.GetValue(EndProperty);
                double elementRange = end - begin;

                Size size = new Size
                {
                    Width =
                        (Orientation == Orientation.Vertical)
                            ? finalSize.Width
                            : elementRange/containerRange*finalSize.Width,
                    Height =
                        (Orientation == Orientation.Vertical)
                            ? elementRange/containerRange*finalSize.Height
                            : finalSize.Height
                };

                Debug.Print((begin - Minimum) / containerRange * finalSize.Width + " " + (begin - Minimum) / containerRange * finalSize.Height);

                Point location = new Point
                {
                    X = (Orientation == Orientation.Vertical) ? 0 : (begin - Minimum)/containerRange*finalSize.Width,
                    Y = (Orientation == Orientation.Vertical) ? (begin - Minimum)/containerRange*finalSize.Height : 0
                };

                element.Arrange(new Rect(location, size));
            }

            return finalSize;
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            foreach (UIElement element in Children)
            {
                element.Measure(availableSize);
            }

            double maxX = 0, maxY = 0;
            foreach (UIElement element in Children)
            {
                if (element.DesiredSize.Width > maxX)
                    maxX = element.DesiredSize.Width;
                if (element.DesiredSize.Height > maxY)
                    maxY = element.DesiredSize.Height;
            }
            availableSize.Width = maxX;
            availableSize.Height = maxY;

            return availableSize;
        }
    }
}

当我运行它时,我收到错误

  

'开始'物业已经由UIElement'

注册

指的是开始/结束属性,但我不知道为什么。

min和max被设置为静态值0/1440并且开始和结束是通过绑定设置的(starttime / endtime是日期时间,它被转换为自午夜以来的分钟数,我< ve; ve还验证了转换器工作属性)如下

<Border BorderBrush="DarkSlateGray"
    RangePanel.Begin="{Binding StartTime, Converter={StaticResource TimeToMinutes}}"
    RangePanel.End="{Binding EndTime, Converter={StaticResource TimeToMinutes}}">

    <TextBlock Text="{Binding AppointmentDescription}" Background="Red" />
</Border>

但是绑定不起作用,这意味着布局失败并且所有元素都堆叠在一起,因为begin始终为0,因此位置变为(0,0)。我已经将它缩小到开始和结束属性没有设置/有值但我无法弄清楚原因。

1 个答案:

答案 0 :(得分:-1)

我找到了自己问题的答案,它位于this answer

基本上,itemscontrol需要通过ItemsControl.ItemContainerStyle设置开始/结束属性。

以下是修改后代码的片段

<ItemsControl.ItemContainerStyle>
    <Style>
        <Setter Property="calendarControls:RangePanel.Begin" Value="{Binding StartTime, Converter={StaticResource TimeToMinutes}}" />
        <Setter Property="calendarControls:RangePanel.End" Value="{Binding EndTime, Converter={StaticResource TimeToMinutes}}" />
    </Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Border BorderBrush="DarkSlateGray">
            <TextBlock Text="{Binding AppointmentDescription}" />
        </Border>
    </DataTemplate>
</ItemsControl.ItemTemplate>

现在按预期工作。