これは、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));
}
}
結果: