更改天数时TimeSpanUpDown的显示错误的解决方法

时间:2015-10-23 10:29:26

标签: wpf wpftoolkit timespan

当天数从0变为> 0时,TimeSpanUpDown(扩展WPF工具包)似乎有显示错误。

这是一种重现它的简单方法:

obja == objb

输入接近但低于24小时的时间跨度。天数会自动隐藏。

enter image description here

然后按第一个控件上的向上箭头将时间跨度增加到> 24h。控件现在更新其显示以包括天数。第二个控件接收属性更改通知并尝试更新,但最终处于一个奇怪的状态:

enter image description here

显然,这是一个错误,应该由Xceed修复,但有没有人知道快速简便的修复或解决方法?

1 个答案:

答案 0 :(得分:0)

为什么不自己滚?欢迎来到自定义控件创作!没有上述错误的简单起点,您可以自定义内容:

[TemplatePart(Name = "UP", Type = typeof(ButtonBase))]
[TemplatePart(Name = "DOWN", Type = typeof(ButtonBase))]
public class TimeSpinner : Control
{
    static TimeSpinner()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(TimeSpinner), new FrameworkPropertyMetadata(typeof(TimeSpinner)));
    }

    public TimeSpan Time
    {
        get { return (TimeSpan)GetValue(TimeProperty); }
        set { SetValue(TimeProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Time.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TimeProperty =
        DependencyProperty.Register("Time", typeof(TimeSpan), typeof(TimeSpinner), new PropertyMetadata(TimeSpan.Zero));

    public TimeSpan Interval
    {
        get { return (TimeSpan)GetValue(IntervalProperty); }
        set { SetValue(IntervalProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Interval.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IntervalProperty =
        DependencyProperty.Register("Interval", typeof(TimeSpan), typeof(TimeSpinner), new PropertyMetadata(TimeSpan.FromTicks(TimeSpan.TicksPerHour)));

    private static readonly TimeSpan OneDay = TimeSpan.FromTicks(TimeSpan.TicksPerDay);

    void UpTime()
    {
        var newTime = Time + Interval;
        if (newTime >= OneDay)
            Time = newTime - OneDay;
        else
            Time = newTime;
    }

    void DownTime()
    {
        var newTime = Time - Interval;
        if (newTime < TimeSpan.Zero)
            Time = newTime + OneDay;
        else
            Time = newTime;
    }


    public override void OnApplyTemplate()
    {
        var upButton = GetUIPart<ButtonBase>("UP");
        if(upButton != null)
            upButton.Click += upButton_Click;
        if (_upButton != null)
            _upButton.Click -= upButton_Click;
        _upButton = upButton;

        var downButton = GetUIPart<ButtonBase>("DOWN");
        if (downButton != null)
            downButton.Click += downButton_Click;
        if (_downButton != null)
            _downButton.Click -= downButton_Click;
        _downButton = downButton;
    }

    void downButton_Click(object sender, RoutedEventArgs e)
    {
        DownTime();
    }

    void upButton_Click(object sender, RoutedEventArgs e)
    {
        UpTime();
    }

    private ButtonBase _upButton;
    private ButtonBase _downButton;

    T GetUIPart<T>(string name) where T : DependencyObject
    {
        return (T) GetTemplateChild(name);
    }
}

和一个示例模板(应该在项目根目录下名为Themes的文件夹中放入名为Generic.xaml的ResourceDictionary):

<Style TargetType="{x:Type local:TimeSpinner}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:TimeSpinner}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <DockPanel>
                        <UniformGrid Columns="1" DockPanel.Dock="Right">
                            <Button x:Name="UP" Content="+"/>
                            <Button x:Name="DOWN" Content="-"/>
                        </UniformGrid>
                        <TextBox Text="{Binding Time, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>
                    </DockPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>