简体   繁体   English

我可以将RangeSlider(来自WPF扩展工具包)绑定到ObservableCollection的datetime属性吗?

[英]Can I bind a RangeSlider (from WPF extended toolkit) to a datetime property of ObservableCollection?

I need to present a range slider of dates (WPF). 我需要显示日期范围滑块(WPF)。 Is it possible to bind a RangeSlider to a DateTime property in an ObservableCollection ? 是否有可能一个绑定RangeSliderDateTime在财产ObservableCollection

EDIT: I tried the following with no success: 编辑:我尝试以下没有成功:

        <xctk:RangeSlider HorizontalAlignment="Left" Margin="101,10,0,0" VerticalAlignment="Top" Width="233" Maximum="{Binding MaxRange}" Minimum="{Binding MinRange}"/>

MinRange and MaxRange are DateTime objects in my ViewModel MinRange和MaxRange是我的ViewModel中的DateTime对象

No. DateTime is a value type, and therefore immutable. 不能。DateTime是一个值类型,因此是不可变的。 And bindings cannot replace an object at index N within a collection with a different object. 绑定不能将集合中索引N处的对象替换为其他对象。

Bindings can alter the value of properties. 绑定可以更改属性的值。 That's all they do. 那就是他们所做的一切。 Period. 期。

In your case, if you wish to have a range slider describe a period of time, you need to create a class which has a start value and an end value exposed as properties. 在您的情况下,如果希望使用范围滑块来描述一段时间,则需要创建一个类,该类具有作为属性公开的开始值和结束值。

public class Range : INotifyPropertyChanged
{
    // INPC impl omitted because this is c# like pseudocode
    public double StartTime {get;set;}
    public double EndTime {get;set;}
    public TimeSpan ToTimeSpan() { return YourConversionLogicLol(); }
}

IIRC, range slider values are doubles. IIRC,范围滑块值是两倍。 It's up to you to define what the double values mean in terms of time, and convert from the values bound in the UI to whatever you need (Range != DateTime, a range is two points in time--start & end, so you confuse me). 由您决定时间上的double值是什么意思,并将UI中绑定的值转换为所需的值(Range!= DateTime,范围为两个时间点-开始和结束,因此,让我感到困惑)。 Ticks from the epoch, or sommet, I dunno. 我不知道从时代开始,或者在奏鸣曲。

You can use OADate of DateTime. 您可以使用DateTime的OADate。 This method convert datetime to double value. 此方法将datetime转换为double值。

Xaml code: XAML代码:

  <xctk:RangeSlider x:Name="_rangeSlider"


                          Minimum="{Binding StartPointOfSlider, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                          Maximum="{Binding StopPointOfSlider}"
                          LowerValue="{Binding StartTimeOnSlide}"
                          HigherValue="{Binding StopTimeOnSlide}"
                          Step="1"
                          Orientation="Horizontal"
                          LowerThumbBackground="#FFF0F0F0"
                          HigherThumbBackground="#FFF0F0F0"
                          RangeBackground="Transparent"
                          HigherRangeBackground="Transparent"
                          LowerRangeBackground="Transparent">

        </xctk:RangeSlider>

And the code behind: 以及后面的代码:

        public double StartTimeOnSlide
    {
        get { return _startTimeOnSlide; }
        set
        {
            Set(() => StartTimeOnSlide, ref _startTimeOnSlide, value);

                SetSelectedGrapHStartIntervalExecute();


        }
    }

    public double StopTimeOnSlide
    {
        get { return _stopTimeOnSlide; }
        set
        {
            Set(() => StopTimeOnSlide, ref _stopTimeOnSlide, value);
            SetSelectedGrapHStopIntervalExecute();
        }
    }

    public double StartPointOfSlider
    {
        get { return _startPointOfSlider; }
        set
        {
            Set(() => StartPointOfSlider, ref _startPointOfSlider, value);
            RaisePropertyChanged();
        }
    }

    public double StopPointOfSlider
    {
        get { return _stopPointOfSlider; }
        set
        {
            Set(() => StopPointOfSlider, ref _stopPointOfSlider, value);
            RaisePropertyChanged();
        }
    }

And the intizial part: 和内部部分:

StartTimeOnSlide = StartDateTime.ToOADate();
            StopTimeOnSlide = StopDateTime.ToOADate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM