我是WPF和c#语言的新手,如果你能帮助我,我感激不尽。
我有两个DateEdit控件和一个GridControl。
我想在第一个日期编辑中指定一个随机日期,并且应该从该日期开始分配2年的DateEdit年份。
应该为GridControl分配相同的日期。
代码是:
private void mTxtDateEditFchIni_EditValueChanged(object sender, EditValueChangedEventArgs e)
{
DateTime fecha = DateTime.Now.AddYears(1);
mTxtDateEditFchFin.Text = fecha.ToString();
}
mTxtDateEditfchIni是第一个带有事件编辑值的文本编辑
我希望首先在任何日期输入日期,并自动将第二个日期(mTxtDateEditFchFin.Text)放入第一个日期的差异年份
答案 0 :(得分:0)
这是你在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="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>
代码背后:
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)});
}
}
数据项:
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));
}
}
结果:
File -> New Project -> WPF Application
中,然后自行查看结果。