//Customized UnBoundRowCellRenderer
public class DatePickerRenderer : GridUnBoundRowCellRenderer<TextBlock, DatePicker>
{
public DatePickerRenderer()
{
}
protected override TextBlock OnCreateDisplayUIElement()
{
return new TextBlock();
}
protected override DatePicker OnCreateEditUIElement()
{
return new DatePicker();
}
public override void OnInitializeDisplayElement(DataColumnBase dataColumn, TextBlock uiElement, object dataContext)
{
uiElement.Text = dataColumn.GridUnBoundRowEventsArgs.Value.ToString();
}
public override void OnUpdateDisplayBinding(DataColumnBase dataColumn, TextBlock uiElement, object dataContext)
{
uiElement.Text = dataColumn.GridUnBoundRowEventsArgs.Value.ToString();
}
#region Edit Element
public override void OnInitializeEditElement(DataColumnBase dataColumn, DatePicker uiElement, object dataContext)
{
uiElement.SelectedDate = (DateTime?)dataColumn.GridUnBoundRowEventsArgs.Value;
uiElement.Tag = dataColumn;
}
public override void OnUpdateEditBinding(DataColumnBase dataColumn, DatePicker element, object dataContext)
{
element.SelectedDate = (DateTime?)dataColumn.GridUnBoundRowEventsArgs.Value;
element.Tag = dataColumn;
}
protected override void OnWireEditUIElement(DatePicker uiElement)
{
base.OnWireEditUIElement(uiElement);
uiElement.SelectedDateChanged += uiElement_SelectedDateChanged;
}
public void uiElement_SelectedDateChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
var datapicker = sender as DatePicker;
if (datapicker.Tag is DataColumn)
{
var dataColumn = datapicker.Tag as DataColumn;
dataColumn.GridUnBoundRowEventsArgs.Value = (sender as DatePicker).SelectedDate;
DataGrid.RaiseQueryUnBoundRow(dataColumn.GridUnBoundRowEventsArgs.GridUnboundRow, UnBoundActions.CommitData, dataColumn.GridUnBoundRowEventsArgs.Value, dataColumn.GridColumn, dataColumn.GridUnBoundRowEventsArgs.CellType, new Syncfusion.UI.Xaml.ScrollAxis.RowColumnIndex(dataColumn.RowIndex, dataColumn.ColumnIndex));
}
}
#endregion
#region Update
protected override void OnEditingComplete(DataColumnBase dataColumn, FrameworkElement currentRendererElement)
{
dataColumn.GridUnBoundRowEventsArgs.Value = (currentRendererElement as DatePicker).Text;
DataGrid.RaiseQueryUnBoundRow(dataColumn.GridUnBoundRowEventsArgs.GridUnboundRow, UnBoundActions.CommitData, dataColumn.GridUnBoundRowEventsArgs.Value, dataColumn.GridColumn, dataColumn.GridUnBoundRowEventsArgs.CellType, new Syncfusion.UI.Xaml.ScrollAxis.RowColumnIndex(dataColumn.RowIndex, dataColumn.ColumnIndex));
}
#endregion
}
…
public MainWindow()
{
InitializeComponent();
sfDataGrid.QueryUnBoundRow += sfDataGrid_QueryUnBoundRow;
sfDataGrid.UnBoundRowCellRenderers.Add("DatePickerRenderer", new DatePickerRenderer());
}
private void sfDataGrid_QueryUnBoundRow(object sender,
Syncfusion.UI.Xaml.Grid.GridUnBoundRowEventsArgs e)
{
if (e.UnBoundAction == UnBoundActions.QueryData)
{
if (e.RowColumnIndex.ColumnIndex == 0)
{
e.CellType = "DatePickerRenderer";
e.Value = DateTime.Now;
e.Handled = true;
}
}
} |
//Customized UnBoundRowCellRenderer
public class GridUnBoundRowCellTextBoxRendererExt : GridUnBoundRowCellRenderer<TextBlock, ComboBox>
{
public GridUnBoundRowCellTextBoxRendererExt()
{
}
protected override TextBlock OnCreateDisplayUIElement()
{
return new TextBlock();
}
/// <summary>
/// Edit Element creation.
/// </summary>
/// <returns></returns>
protected override ComboBox OnCreateEditUIElement()
{
return new ComboBox();
}
public override void OnInitializeDisplayElement(DataColumnBase dataColumn, TextBlock uiElement, object dataContext)
{
uiElement.Text = dataColumn.GridUnBoundRowEventsArgs.Value.ToString();
}
/// <summary>
/// Updates the value for display element.
/// </summary>
/// <param name="dataColumn"></param>
/// <param name="uiElement"></param>
/// <param name="dataContext"></param>
public override void OnUpdateDisplayBinding(DataColumnBase dataColumn, TextBlock uiElement, object dataContext)
{
uiElement.Text = dataColumn.GridUnBoundRowEventsArgs.Value.ToString();
}
#region Edit Element
ComboBox combo = new ComboBox();
protected override void OnEditElementLoaded(object sender, RoutedEventArgs e)
{
var displayValue = combo.SelectedValue;
combo = sender as ComboBox;
List<string> itemsCollection = new List<string>();
itemsCollection.Add("India");
itemsCollection.Add("USA");
combo.ItemsSource = itemsCollection;
combo.SelectionChanged += combo_SelectionChanged;
combo.SelectedValue = displayValue.ToString();
}
void combo_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
combo = sender as ComboBox;
if (combo != null && combo.SelectedItem != null)
{
(DataGrid.DataContext as SalesInfoViewModel).DisplayValue = combo.SelectedItem.ToString();
var binding = new Binding();
binding.Converter = new CurrencyConverter();
binding.ConverterParameter = combo.SelectedItem.ToString();
DataGrid.Columns[2].DisplayBinding = binding;
DataGrid.Columns[2].ValueBinding = binding;
}
}
/// <summary>
/// Initialize the value for edit element.
/// </summary>
/// <param name="dataColumn"></param>
/// <param name="uiElement"></param>
/// <param name="dataContext"></param>
public override void OnInitializeEditElement(DataColumnBase dataColumn, ComboBox uiElement, object dataContext)
{
if(dataColumn.GridUnBoundRowEventsArgs.Value!=null)
combo.SelectedValue = dataColumn.GridUnBoundRowEventsArgs.Value.ToString();
}
void uiElement_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
var comboBox = sender as ComboBox;
if (comboBox != null && comboBox.SelectedItem != null)
{
(DataGrid.DataContext as SalesInfoViewModel).DisplayValue = comboBox.SelectedItem.ToString();
var binding = new Binding();
binding.Converter = new CurrencyConverter();
binding.ConverterParameter = comboBox.SelectedItem.ToString();
DataGrid.Columns[2].DisplayBinding = binding;
DataGrid.Columns[2].ValueBinding = binding;
//(DataGrid.Columns[2].DisplayBinding as Binding).ConverterParameter = comboBox.SelectedItem.ToString();
//(DataGrid.Columns[2].DisplayBinding as Binding).Converter = new CurrencyConverter();
}
}
public override void OnUpdateEditBinding(DataColumnBase dataColumn, ComboBox element, object dataContext)
{
var displayValue = (DataGrid.DataContext as SalesInfoViewModel).DisplayValue;
element.SelectedValue = displayValue;
}
#endregion
#region Update
/// <summary>
/// Update display value and raise event
/// </summary>
/// <param name="dataColumn"></param>
/// <param name="currentRendererElement"></param>
protected override void OnEditingComplete(DataColumnBase dataColumn, FrameworkElement currentRendererElement)
{
dataColumn.GridUnBoundRowEventsArgs.Value = (currentRendererElement as ComboBox).SelectedValue;
DataGrid.RaiseQueryUnBoundRow(dataColumn.GridUnBoundRowEventsArgs.GridUnboundRow, UnBoundActions.CommitData, dataColumn.GridUnBoundRowEventsArgs.Value, dataColumn.GridColumn, dataColumn.GridUnBoundRowEventsArgs.CellType, new Syncfusion.UI.Xaml.ScrollAxis.RowColumnIndex(dataColumn.RowIndex, dataColumn.ColumnIndex));
}
#endregion
…
public MainWindow()
{
InitializeComponent();
sfDataGrid.QueryUnBoundRow += sfDataGrid_QueryUnBoundRow;
sfDataGrid.UnBoundRowCellRenderers.Add("GridUnBoundRowCellTextBoxRendererExt", new GridUnBoundRowCellTextBoxRendererExt());
}
void sfDataGrid_QueryUnBoundRow(object sender, Syncfusion.UI.Xaml.Grid.GridUnBoundRowEventsArgs e)
{
if (e.UnBoundAction == UnBoundActions.QueryData)
{
if (e.GridUnboundRow.UnBoundRowIndex == 0 && e.GridUnboundRow.Position == UnBoundRowsPosition.Top)
{
if (e.RowColumnIndex.ColumnIndex == 2)
{
e.CellType = "GridUnBoundRowCellTextBoxRendererExt";
e.Value = (sfDataGrid.DataContext as SalesInfoViewModel).DisplayValue;
e.Handled = true;
}
}
}
else if(e.UnBoundAction == UnBoundActions.CommitData)
{
if(e.Value!=null)
(sfDataGrid.DataContext as SalesInfoViewModel).DisplayValue = e.Value.ToString();
}
e.Handled = true;
}
private void ComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
var comboBox = sender as ComboBox;
if (comboBox != null && comboBox.SelectedItem != null)
{
(sfDataGrid.DataContext as SalesInfoViewModel).DisplayValue = (comboBox.SelectedItem as ComboBoxItem).Content.ToString();
var binding = new Binding();
binding.Converter = new CurrencyConverter();
binding.ConverterParameter = (comboBox.SelectedItem as ComboBoxItem).Content;
sfDataGrid.Columns[2].DisplayBinding = binding;
sfDataGrid.Columns[2].ValueBinding = binding;
}
}
} |
<Window.Resources>
<local:SalesInfoViewModel x:Key="ViewModel" />
<DataTemplate x:Key="UnBoundRowCellTemplate">
<TextBlock HorizontalAlignment="Right"
VerticalAlignment="Center"
Padding="3"
Text="{Binding}"
TextWrapping="Wrap" />
</DataTemplate>
<DataTemplate x:Key="UnBoundRowEditTemplate">
<ComboBox x:Name="comboBox"
SelectedValue="{Binding}"
SelectionChanged="ComboBox_SelectionChanged">
<ComboBoxItem>India</ComboBoxItem>
<ComboBoxItem>USA</ComboBoxItem>
</ComboBox>
</DataTemplate>
<local:CurrencyConverter x:Key="CurrencyConverter" />
</Window.Resources> |