This question is locked. New answers and comments are not allowed.
Hi all,
Here is the problem;
I have a grid and one column is has to be comboboxcolumn, but this Comboboxcolumn's ItemSource set when some property change event. I've already done it. However, Column's first value always empty, also when I finished editing still cell seems empty.
Briefly,
I wanna create a custom grdiviewecomboboxcolumn.
Features:
Before editing cell sets own value via DataMemberBinding,
On editing, combobox appears with values (ItemSource assinged already)
After editing Selected combobox value assings to cell.
My code is following.
I've read colorpicker example and it's not suitable for me.
Also I tried inheriting GridViewBoundColumnBase but this time cell seem loaded, after changing value with combo cell updates. It seems it's ok, but in CreateCellElement method dataItem doesn't change.
In this case when I confirmed the editing. It saves old values.
I'm trying to change textblock value in CreateCellElement method, I can change but as I wrote earlier dataItem parameter has old value.
Second try inherited from GridViewBoundColumnBase
private Binding itemsSourceBinding;
/// <summary>
/// Gets or sets the Binding for the ComboBoxEdtor ItemsSource. This is a dependency property.
/// </summary>
/// <returns> The default value is null</returns>
public Binding ItemsSourceBinding
{
get
{
return this.itemsSourceBinding;
}
set
{
if (this.itemsSourceBinding != value)
{
this.itemsSourceBinding = value;
}
}
}
/// <summary>
/// Represents the IsComboBoxEditable dependency property
/// </summary>
public static readonly DependencyProperty IsComboBoxEditableProperty =
DependencyProperty.Register("IsComboBoxEditable",
typeof(bool),
typeof(ComboBoxSRV),
null);
/// <summary>
/// Represents the ValueMemberPath dependency property
/// </summary>
public static readonly DependencyProperty SelectedValueMemberPathProperty =
DependencyProperty.Register("SelectedValueMemberPath",
typeof(string),
typeof(ComboBoxSRV),
new PropertyMetadata(string.Empty));
/// <summary>
/// Represents the ItemsSource dependency property
/// </summary>
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource",
typeof(IEnumerable),
typeof(ComboBoxSRV),
null);
/// <summary>
/// Gets or sets a value indicating whether the user can edit text in the text box portion of the ComboBox editor
/// </summary>
public bool IsComboBoxEditable
{
get
{
return (bool)GetValue(IsComboBoxEditableProperty);
}
set
{
SetValue(IsComboBoxEditableProperty, value);
}
}
/// <summary>
/// Gets or sets the value member path.
/// </summary>
/// <value>The value member path.</value>
public string SelectedValueMemberPath
{
get
{
return (string)GetValue(SelectedValueMemberPathProperty);
}
set
{
SetValue(SelectedValueMemberPathProperty, value);
}
}
/// <summary>
/// Gets or sets the item template used to represent the selected item in cells.
/// </summary>
/// <value>The item template.</value>
public DataTemplate ItemTemplate
{
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
set { SetValue(ItemTemplateProperty, value); }
}
/// <summary>
/// Represents the ItemTemplate dependency property
/// </summary>
public static readonly DependencyProperty ItemTemplateProperty =
DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(ComboBoxSRV), new PropertyMetadata(null));
/// <summary>
/// Gets or sets the the list of items used in the column to display lookup data.
/// The ComboBox editor will also attach to this ItemsSource.
/// </summary>
/// <value>The items source.</value>
public IEnumerable ItemsSource
{
get
{
return (IEnumerable)GetValue(ItemsSourceProperty);
}
set
{
this.SetValue(ItemsSourceProperty, value);
}
}
Here is the problem;
I have a grid and one column is has to be comboboxcolumn, but this Comboboxcolumn's ItemSource set when some property change event. I've already done it. However, Column's first value always empty, also when I finished editing still cell seems empty.
Briefly,
I wanna create a custom grdiviewecomboboxcolumn.
Features:
Before editing cell sets own value via DataMemberBinding,
On editing, combobox appears with values (ItemSource assinged already)
After editing Selected combobox value assings to cell.
My code is following.
public class GridViewComboBox : GridViewComboBoxColumn {protected override void OnPropertyChanged(PropertyChangedEventArgs args){base.OnPropertyChanged(args);if (args.PropertyName == "Service")ServiceExecutor.ExecuteService(Service, MethodName, x => ItemsSource = (IEnumerable)x);} public override object GetNewValueFromEditor(object editor){var combo = editor as RadComboBox;return combo != null ? combo.SelectedItem : null;}public override void CopyPropertiesFrom(GridViewColumn source){base.CopyPropertiesFrom(source);var comboColumn = source as GridViewComboBox;if (comboColumn != null){SelectedValueMemberPath = comboColumn.SelectedValueMemberPath;ItemsSource = comboColumn.ItemsSource;ItemsSourceBinding = comboColumn.ItemsSourceBinding;DisplayMemberPath = comboColumn.DisplayMemberPath;LookupValueConverter = comboColumn.LookupValueConverter;IsComboBoxEditable = comboColumn.IsComboBoxEditable;ItemTemplate = comboColumn.ItemTemplate;}}public string Service{get{return (string)GetValue(ServiceProperty);}set{SetValue(ServiceProperty, value);}}public readonly DependencyProperty ServiceProperty = DependencyProperty.Register("Service",typeof(string),typeof(GridViewComboBox),new PropertyMetadata(ServiceChanged));public static void ServiceChanged(DependencyObject obj, DependencyPropertyChangedEventArgs arg){((GridViewComboBox)obj).OnPropertyChanged("Service");}public string MethodName{get{return (string)GetValue(MethodNameProperty);}set{SetValue(MethodNameProperty, value);}}public static readonly DependencyProperty MethodNameProperty =DependencyProperty.Register("MethodName",typeof(string),typeof(GridViewComboBox),new PropertyMetadata("GetList"));}I've read colorpicker example and it's not suitable for me.
Also I tried inheriting GridViewBoundColumnBase but this time cell seem loaded, after changing value with combo cell updates. It seems it's ok, but in CreateCellElement method dataItem doesn't change.
In this case when I confirmed the editing. It saves old values.
I'm trying to change textblock value in CreateCellElement method, I can change but as I wrote earlier dataItem parameter has old value.
Second try inherited from GridViewBoundColumnBase
public sealed class ComboBoxSRV : GridViewBoundColumnBase { public override void CopyPropertiesFrom(GridViewColumn source) { base.CopyPropertiesFrom(source); var comboColumn = source as ComboBoxSRV; if (comboColumn != null) { this.SelectedValueMemberPath = comboColumn.SelectedValueMemberPath; this.ItemsSource = comboColumn.ItemsSource; this.ItemsSourceBinding = comboColumn.ItemsSourceBinding; this.DisplayMemberPath = comboColumn.DisplayMemberPath; //this.LookupValueConverter = comboColumn.LookupValueConverter; this.IsComboBoxEditable = comboColumn.IsComboBoxEditable; this.ItemTemplate = comboColumn.ItemTemplate; } } public override object GetNewValueFromEditor(object editor) { RadComboBox combo = editor as RadComboBox; if (combo != null) { return combo.SelectedItem; } return null; } public override IList<ValidationError> UpdateSourceWithEditorValue(GridViewCell gridViewCell) { List<ValidationError> errors = new List<ValidationError>(); RadComboBox editor = gridViewCell.GetEditingElement() as RadComboBox; BindingExpression bindingExpression = editor.ReadLocalValue(RadComboBox.SelectedItemProperty) as BindingExpression; if (bindingExpression != null) { bindingExpression.UpdateSource(); errors.AddRange(Validation.GetErrors(editor)); } return errors; } public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem) { if (DT != null) { var cellElement = new TextBlock(); var valueBinding = new Binding(DisplayMemberPath) { Source = DT, Mode = BindingMode.OneTime }; cellElement.SetBinding(TextBlock.TextProperty, valueBinding); return cellElement; } return base.CreateCellElement(cell, dataItem); } private Binding CreateValueBinding() { Binding valueBinding = new Binding(); valueBinding.Mode = BindingMode.TwoWay; valueBinding.NotifyOnValidationError = true; valueBinding.ValidatesOnExceptions = true; valueBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit; valueBinding.Path = new PropertyPath(this.DataMemberBinding.Path.Path); return valueBinding; } public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem) { var element = new RadComboBox(); this.BindingTarget = RadComboBox.SelectedItemProperty; Binding valueBinding = CreateValueBinding(); element.SetBinding(RadComboBox.SelectedItemProperty, valueBinding); element.DropDownOpened += new EventHandler(element_DropDownOpened); element.SelectionChanged += new Telerik.Windows.Controls.SelectionChangedEventHandler(element_SelectionChanged); return element; } void element_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e) { if (((RadComboBox)sender).SelectedItem != null) { DT = ((RadComboBox)sender).SelectedItem; //this.OnPropertyChanged("DT"); // Text = ((BaseCodeDT)((RadComboBox)sender).SelectedItem).ItemString;); // this.DataMemberBinding.Source = Item; } } void element_DropDownOpened(object sender, EventArgs e) { ServiceExecutor.ExecuteService(Service, MethodName, x => ((RadComboBox)sender).ItemsSource = (IEnumerable)x); ((RadComboBox)sender).SelectedValuePath = SelectedValuePath; ((RadComboBox)sender).DisplayMemberPath = DisplayMemberPath; } #region "Properties" public string BindingPath { get { return (string)GetValue(BindingPathProperty); } set { SetValue(BindingPathProperty, value); } } public static readonly DependencyProperty BindingPathProperty = DependencyProperty.Register("BindingPath", typeof(string), typeof(ComboBoxSRV), new PropertyMetadata(null)); public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(ComboBoxSRV), new PropertyMetadata(null)); public static void ItemChanged(DependencyObject obj,DependencyPropertyChangedEventArgs arg) { ((ComboBoxSRV)obj).OnPropertyChanged("DT"); } public object DT { get { return GetValue(DTProperty); } set { SetValue(DTProperty, value); } } public static readonly DependencyProperty DTProperty = DependencyProperty.Register("DT", typeof(object), typeof(ComboBoxSRV), new PropertyMetadata(ItemChanged)); public string DisplayMemberPath { get { return (string)GetValue(DisplayMemberPathProperty); } set { SetValue(DisplayMemberPathProperty, value); } } public static readonly DependencyProperty DisplayMemberPathProperty = DependencyProperty.Register("DisplayMemberPath", typeof(string), typeof(ComboBoxSRV), new PropertyMetadata(null)); public string SelectedValuePath { get { return (string)GetValue(SelectedValuePathProperty); } set { SetValue(SelectedValuePathProperty, value); } } public static readonly DependencyProperty SelectedValuePathProperty = DependencyProperty.Register("SelectedValuePath", typeof(string), typeof(ComboBoxSRV), new PropertyMetadata(null)); public string Service { get { return (string)GetValue(ServiceProperty); } set { SetValue(ServiceProperty, value); } } public static readonly DependencyProperty ServiceProperty = DependencyProperty.Register("Service", typeof(string), typeof(ComboBoxSRV), new PropertyMetadata(null)); public string MethodName { get { return (string)GetValue(MethodNameProperty); } set { SetValue(MethodNameProperty, value); } } public static readonly DependencyProperty MethodNameProperty = DependencyProperty.Register("MethodName", typeof(string), typeof(ComboBoxSRV), new PropertyMetadata("GetList")); /// <summary>
/// Gets or sets the Binding for the ComboBoxEdtor ItemsSource. This is a dependency property.
/// </summary>
/// <returns> The default value is null</returns>
public Binding ItemsSourceBinding
{
get
{
return this.itemsSourceBinding;
}
set
{
if (this.itemsSourceBinding != value)
{
this.itemsSourceBinding = value;
}
}
}
/// <summary>
/// Represents the IsComboBoxEditable dependency property
/// </summary>
public static readonly DependencyProperty IsComboBoxEditableProperty =
DependencyProperty.Register("IsComboBoxEditable",
typeof(bool),
typeof(ComboBoxSRV),
null);
/// <summary>
/// Represents the ValueMemberPath dependency property
/// </summary>
public static readonly DependencyProperty SelectedValueMemberPathProperty =
DependencyProperty.Register("SelectedValueMemberPath",
typeof(string),
typeof(ComboBoxSRV),
new PropertyMetadata(string.Empty));
/// <summary>
/// Represents the ItemsSource dependency property
/// </summary>
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource",
typeof(IEnumerable),
typeof(ComboBoxSRV),
null);
/// <summary>
/// Gets or sets a value indicating whether the user can edit text in the text box portion of the ComboBox editor
/// </summary>
public bool IsComboBoxEditable
{
get
{
return (bool)GetValue(IsComboBoxEditableProperty);
}
set
{
SetValue(IsComboBoxEditableProperty, value);
}
}
/// <summary>
/// Gets or sets the value member path.
/// </summary>
/// <value>The value member path.</value>
public string SelectedValueMemberPath
{
get
{
return (string)GetValue(SelectedValueMemberPathProperty);
}
set
{
SetValue(SelectedValueMemberPathProperty, value);
}
}
/// <summary>
/// Gets or sets the item template used to represent the selected item in cells.
/// </summary>
/// <value>The item template.</value>
public DataTemplate ItemTemplate
{
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
set { SetValue(ItemTemplateProperty, value); }
}
/// <summary>
/// Represents the ItemTemplate dependency property
/// </summary>
public static readonly DependencyProperty ItemTemplateProperty =
DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(ComboBoxSRV), new PropertyMetadata(null));
/// <summary>
/// Gets or sets the the list of items used in the column to display lookup data.
/// The ComboBox editor will also attach to this ItemsSource.
/// </summary>
/// <value>The items source.</value>
public IEnumerable ItemsSource
{
get
{
return (IEnumerable)GetValue(ItemsSourceProperty);
}
set
{
this.SetValue(ItemsSourceProperty, value);
}
}
#endregion }