I have a custom GridView inherited from ‘RadGridView’ which is binded to generic list.
Some of the rows in the gridview is becoming readonly(Edited values are getting restored to older value when curser moved to next position) while editing(not all rows).
Please help
Custom grid view
------------
public enum EquipmentList
{
Unknown,
StowList,
DischargeList,
RestowList,
HandList,
StowedList,
ProcessList
}
public class EquipmentsGridView : RadGridView
{
public EquipmentList ListType { get; set; }
private ObservableCollection<EquipmentInformation> _source;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "By design")]
public IList<EquipmentInformation> Source
{
get
{
return _source;
}
set
{
if (_source != null)
{
_source.CollectionChanged -= new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_source_CollectionChanged);
}
base.ItemsSource = _source = new ObservableCollection<EquipmentInformation>(value);
_source.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_source_CollectionChanged);
ResetNumbers();
ApplyBehavior();
}
}
public bool IsEdited { get; set; }
public EquipmentsGridView()
{
base.CellValidating += GridView_CellValidating;
base.SelectionChanged += new EventHandler<SelectionChangeEventArgs>(EquipmentsGridView_SelectionChanged);
}
void EquipmentsGridView_SelectionChanged(object sender, SelectionChangeEventArgs e)
{
if (this.Items.IsAddingNew || this.Items.IsEditingItem)
{
this.CommitEdit();
}
}
private void ApplyBehavior()
{
foreach (var column in base.Columns)
{
if (column is GridViewBoundColumnBase)
{
column.ShowDistinctFilters = false;
}
}
}
void EquipmentsGridView_CellValidated(object sender, GridViewCellValidatedEventArgs e)
{
if (e.Cell.ParentRow.Item != null)
{
if (((EquipmentInformation)e.Cell.ParentRow.Item).IsCanceled)
{
e.Cell.Foreground = Brushes.White;
e.Cell.Background = Brushes.Red;
e.Cell.FontStyle = FontStyles.Oblique;
}
else
{
e.Cell.Foreground = Brushes.Black;
e.Cell.Background = Brushes.White;
e.Cell.FontStyle = FontStyles.Normal;
}
}
}
void _source_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add ||
e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove ||
e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset)
{
ResetNumbers();
IsEdited = true;
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Justification = "Editing need to be in lowercase")]
private void GridView_CellValidating(object sender, Telerik.Windows.Controls.GridViewCellValidatingEventArgs e)
{
if (e.OldValue != e.NewValue)
{
((System.Windows.Controls.TextBox)(e.EditingElement)).Text = e.NewValue.ToString().ToLower(System.Globalization.CultureInfo.InvariantCulture);
IsEdited = true;
}
}
}
public
static
void
TextChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
double
dub;
if
(e.OldValue !=
null
&& e.NewValue !=
null
&& (e.NewValue
is
double
|| (e.NewValue
is
string
&&
double
.TryParse(e.NewValue.ToString(),
out
dub))))
{
var control = o
as
ValueCellPresenter;
if
(control.IsLoaded)
{
((Storyboard)control.Resources[
"CellFlash"
]).Begin();
}
}
}
<telerik:RadTreeView HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="CategoryTree" ItemsSource="{Binding Path=Categories}" SelectedItem="{Binding Path=CurrentCategory, Mode=TwoWay}" SelectionChanged="CategoryTree_SelectionChanged">
<telerik:RadTreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Categories}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</telerik:RadTreeView.ItemTemplate>
</telerik:RadTreeView>
protected override void Load()As you can see, I iterate through root level categories and they all have a Categories property filled with data. But when I try to select child categories from tree, they select visually on the tree but SelectedItem is always null. This happens because in Load() method I have CollectionViewSource.GetDefaultView(Categories) which returns a list which contains only root categories.
{
_categories = new ObservableCollection<CategoryBO>();
foreach (Category category in _repositoryCategory.Entities.Where(p => p.ParentCategory == null))
{
CategoryBO tempCategory = category;
if (tempCategory != null)
{
Categories.Add(tempCategory);
}
}
_categoryView = CollectionViewSource.GetDefaultView(Categories);
OnPropertyChanged("CategoriesList");
OnPropertyChanged("CurrentCategory");
} and this is how I initialize a CategoryBO object:public static implicit operator CategoryBO(Category category)
{
return new CategoryBO(category);
}public CategoryBO(Category category)
{
ID = category.CategoryID;
ParentID = category.ParentID;
Name = category.Name;
foreach (ProductBO product in category.Products)
{
_products.Add(product);
}
foreach (Category cat in category.Categories)
{
_categories.Add(cat);
}
}
<telerik:GridViewDataColumn
ShowDistinctFilters="False"
Width="250"
>
<telerik:GridViewColumn.Header>
<TextBlock
Text="Role Name"
Style="{StaticResource HeaderTextBlockStyle}"
>
</TextBlock>
</telerik:GridViewColumn.Header>
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock
Text="{Binding Path=RoleName}"
TextTrimming="CharacterEllipsis"
Width="250"
>
<TextBlock.ToolTip>
<ToolTip
Content="{Binding Path=RoleName}"
Background="{StaticResource BlueBrush}"
Style="{StaticResource ToolTipStyle}"
>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewDataColumn>