728x90
728x170

기간으로 표시하기위한 Converter 추가

DateToDaysConverter.cs

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

namespace Wpf.DevGridTest
{
    internal class DateToDaysConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (System.Convert.ToDateTime(value) - DateTime.Now).Days;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

Resource 에 Converter 정의 및 DateEdit 에 적용
MainWindow.xaml

xmlns:local="clr-namespace:Wpf.DevGridTest"    

    <Window.Resources>
        <local:DateToDaysConverter x:Key="DateToDaysConverter" />
    </Window.Resources>

    <StackPanel Grid.Row="2" Orientation="Horizontal">
            <dxe:DateEdit
                Width="90"
                Height="30"
                VerticalAlignment="Center"
                HorizontalContentAlignment="Right"
                DisplayTextConverter="{StaticResource DateToDaysConverter}"
                EditValue="{Binding FromDate}" />
            <Label
                VerticalAlignment="Center"
                HorizontalContentAlignment="Right"
                Content="~" />
            <dxe:DateEdit
                Width="90"
                Height="30"
                VerticalAlignment="Center"
                HorizontalContentAlignment="Right"
                DisplayTextConverter="{StaticResource DateToDaysConverter}"
                EditValue="{Binding ToDate}" />
    </StackPanel>

MainViewModel.cs

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;

namespace Wpf.DevGridTest
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged([CallerMemberName] string name = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));

        private DateTime fromDate;

        public DateTime FromDate
        {
            get => this.fromDate;
            set
            {
                this.fromDate = value;
                OnPropertyChanged();
            }
        }

        private DateTime toDate;

        public DateTime ToDate
        {
            get => this.toDate;
            set
            {
                this.toDate = value;
                OnPropertyChanged();
            }
        }

        public MainViewModel()
        {
            FromDate = DateTime.Now.AddDays(-10);
            ToDate = DateTime.Now;
        }
    }
}

결과

728x90
그리드형
Posted by kjun.kr
,