WPF 滑块和日期

WPF Slider and dates(WPF 滑块和日期)
本文介绍了WPF 滑块和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作滑块来选择日期.例如,过去两天的每个小时.滑块底部应该有一个带有值的图例.我该怎么办?

I want to make slider to select dates. For example, every hour in last two days. Also slider should has a legend on the bottom with values. How could I do it?

我根据日期的总小时数制作了带有数据上下文的滑块作为 DoubleCollection,并使用自定义 ValueConverter 更改了工具提示.但是当我更改值时,工具提示会显示实际值 - 日期的总小时数.我也不知道如何添加图例.

I made slider with data context as DoubleCollection from total hours in date and changed tooltip using custom ValueConverter. But when I change value, tooltip shows real values - total hours in date. Also I have no idea how to add a legend.

推荐答案

这是一个工作示例.首先,我们创建一个从 0 到 48 舍入为整数值 (TickFrequency="1" IsSnapToTickEnabled="True") 的滑块,然后添加一个绑定到滑块值的 TextBlock.

Here is a working example. First we create a slider from 0 to 48 rounded to integer values (TickFrequency="1" IsSnapToTickEnabled="True") then add a TextBlock bound to the slider value.

ValueConverter用于将0-48的值转换成日期.

A ValueConverter is used to convert the 0-48 value into a date.

<Window x:Class="StackOverflow2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:StackOverflow2"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:HourToDateConverter x:Key="MyHourConverter"/>
    </Window.Resources>
    <StackPanel>
        <Slider x:Name="MySlider" Minimum="0" Maximum="48" TickFrequency="1" IsSnapToTickEnabled="True"/>
        <TextBlock Text="{Binding ElementName=MySlider, Path=Value, Converter={StaticResource MyHourConverter}}" HorizontalAlignment="Center"/>
    </StackPanel>
</Window>

以及背后的代码:

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace StackOverflow2
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
    public class HourToDateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            object result = DependencyProperty.UnsetValue;
            if (value is double)
                result = DateTime.Now.Date.AddHours((double)value);
            return result;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}

这篇关于WPF 滑块和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)