本文介绍了如何使用随机日期启动DateEdit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有两个DateEdit控件和一个GridControl。



我想在第一个日期编辑中分配一个随机日期,第二个DateEdit应该分配一年的差异,从这个日期开始。



相同的日期应该分配给GridControl。



代码是:

  private void mTxtDateEditFchIni_EditValueChanged(object sender,EditValueChangedEventArgs e)
{

DateTime fecha = DateTime.Now.AddYears(1);
mTxtDateEditFchFin.Text = fecha.ToString();

}

mTxtDateEditfchIni是第一个文本编辑与事件editvalue

我想要一个日期在第一个任何日期,并自动第二个日期(mTxtDateEditFchFin.Text)放置一年的第一个dateedit的差异

解决方案

这是在WPF(与DevExpress)中这样做的方式:

 < Window x:Class =MiscSamples.DateRangeGridSample
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml
xmlns:dxe =http://schemas.devexpress.com/winfx/2008/xaml/editors
xmlns:dxg =http://schemas.devexpress.com/winfx/2008/xaml/grid
Title =DateRangeGridSampleHeight =300Width =300>
& DockPanel>
< StackPanel DockPanel.Dock =Top>
< Label Content =开始日期:/>
< dxe:DateEdit EditValue ={Binding FocusedRow.StartDate,ElementName = Table}/>
< Label Content =结束日期:/>
< dxe:DateEdit EditValue ={Binding FocusedRow.EndDate,ElementName = Table}/>
< Button Content =NewClick =NewRowMargin =10/>
< / StackPanel>

< dxg:GridControl ItemsSource ={Binding}>
< dxg:GridControl.View>
< dxg:TableView x:Name =TableShowGroupPanel =False/>
< / dxg:GridControl.View>
< dxg:GridControl.Columns>
< dxg:GridColumn FieldName =StartDate/>
< dxg:GridColumn FieldName =EndDate/>
< / dxg:GridControl.Columns>
< / dxg:GridControl>
< / DockPanel>
< / Window>

代码背后:

  public partial class DateRangeGridSample:Window 
{
public ObservableCollection< DateRange>数据{get;组; }

public DateRangeGridSample()
{
InitializeComponent();

DataContext = Data = new ObservableCollection< DateRange>();
}

private void NewRow(object sender,RoutedEventArgs e)
{
Data.Add(new DateRange(){StartDate = DateTime.Today,EndDate = DateTime .Today.AddYears(1)});
}
}

数据项:

  public class DateRange:INotifyPropertyChanged 
{
private DateTime _startDate;
private DateTime _endDate;

public DateTime StartDate
{
get {return _startDate; }
set
{
_startDate = value;
OnPropertyChanged(StartDate);
}
}

public DateTime EndDate
{
get {return _endDate; }
set
{
_endDate = value;
OnPropertyChanged(EndDate);
}
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if(handler!= null)handler(this,new PropertyChangedEventArgs(propertyName));
}
}

结果:






  • 您不会(通常)处理WPF中的任何事件(Button Click除外,也可以由

  • 一切都是通过。你真的需要知道,如果你使用WPF。

  • WPF Rocks只需复制并粘贴我的代码一个文件 - >新项目 - > WPF应用程序,并自己查看结果。


Im new in WPF and c# language, And I appreciate if you can help me.

I have two DateEdit controls and a GridControl.

I want to assign a random date in first Date edit, and the 2'nd DateEdit should be assigned with a year diferrence and from this date.

The same dates should be assigned to the GridControl.

The code is:

    private void mTxtDateEditFchIni_EditValueChanged(object sender, EditValueChangedEventArgs e)
    {

        DateTime fecha = DateTime.Now.AddYears(1);
        mTxtDateEditFchFin.Text = fecha.ToString();

    }

mTxtDateEditfchIni is the first text edit with the event editvalue

I want a to put a date in first any date and automatically the 2nd date ( mTxtDateEditFchFin.Text) put a year of diferrence of first dateedit

解决方案

This is how you do that in WPF (with DevExpress):

<Window x:Class="MiscSamples.DateRangeGridSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
        xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
        Title="DateRangeGridSample" Height="300" Width="300">
    <DockPanel>
        <StackPanel DockPanel.Dock="Top">
            <Label Content="Start Date:"/>
            <dxe:DateEdit EditValue="{Binding FocusedRow.StartDate, ElementName=Table}"/>
            <Label Content="End Date:"/>
            <dxe:DateEdit EditValue="{Binding FocusedRow.EndDate, ElementName=Table}"/>
            <Button Content="New" Click="NewRow" Margin="10"/>
        </StackPanel>

        <dxg:GridControl ItemsSource="{Binding}">
            <dxg:GridControl.View>
                <dxg:TableView x:Name="Table" ShowGroupPanel="False"/>
            </dxg:GridControl.View>
            <dxg:GridControl.Columns>
                <dxg:GridColumn FieldName="StartDate"/>
                <dxg:GridColumn FieldName="EndDate"/>
            </dxg:GridControl.Columns>
        </dxg:GridControl>
    </DockPanel>
</Window>

Code Behind:

public partial class DateRangeGridSample : Window
{
    public ObservableCollection<DateRange> Data { get; set; }

    public DateRangeGridSample()
    {
        InitializeComponent();

        DataContext = Data = new ObservableCollection<DateRange>();
    }

    private void NewRow(object sender, RoutedEventArgs e)
    {
        Data.Add(new DateRange() {StartDate = DateTime.Today, EndDate = DateTime.Today.AddYears(1)});
    }
}

Data Item:

public class DateRange: INotifyPropertyChanged
{
    private DateTime _startDate;
    private DateTime _endDate;

    public DateTime StartDate
    {
        get { return _startDate; }
        set
        {
            _startDate = value;
            OnPropertyChanged("StartDate");
        }
    }

    public DateTime EndDate
    {
        get { return _endDate; }
        set
        {
            _endDate = value;
            OnPropertyChanged("EndDate");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

Result:

  • You don't (normally) handle any events in WPF (except for the Button Click, which could also be replaced by a Command
  • Everything is done via DataBinding. You really need to learn that if you're using WPF.
  • WPF Rocks. Just copy and paste my code in a File -> New Project -> WPF Application and see the results for yourself.

这篇关于如何使用随机日期启动DateEdit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 05:45