Error syncfusion navigationItem
unknown
csharp
a year ago
24 kB
1
Indexable
Never
using System; using System.Collections.Specialized; using System.ComponentModel; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; using System.Windows.Markup; using System.Windows.Media; namespace Syncfusion.UI.Xaml.NavigationDrawer { [ContentProperty("Items")] [ToolboxItem(false)] [StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(NavigationItem))] public class NavigationItem : HeaderedItemsControl, IDisposable { internal static readonly DependencyProperty IndentationWidthProperty = DependencyProperty.Register("IndentationWidth", typeof(double), typeof(NavigationItem), new PropertyMetadata(0.0, OnIndentationWidthPropertyChanged)); public static readonly DependencyProperty IconTemplateProperty = DependencyProperty.Register("IconTemplate", typeof(DataTemplate), typeof(NavigationItem), new PropertyMetadata(null)); public static readonly DependencyProperty IconMemberPathProperty = DependencyProperty.Register("IconMemberPath", typeof(string), typeof(NavigationItem), new PropertyMetadata(string.Empty)); public static readonly DependencyProperty ExpanderTemplateProperty = DependencyProperty.Register("ExpanderTemplate", typeof(DataTemplate), typeof(NavigationItem), new PropertyMetadata(null)); public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(object), typeof(NavigationItem), new PropertyMetadata(null)); public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(NavigationItem), new PropertyMetadata(null)); public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(NavigationItem), new PropertyMetadata(null)); public static readonly DependencyProperty IsChildSelectedProperty = DependencyProperty.Register("IsChildSelected", typeof(bool), typeof(NavigationItem), new PropertyMetadata(false)); public static readonly DependencyProperty ItemTypeProperty = DependencyProperty.Register("ItemType", typeof(ItemType), typeof(NavigationItem), new PropertyMetadata(ItemType.Tab)); public static readonly DependencyProperty IsExpandedProperty = DependencyProperty.Register("IsExpanded", typeof(bool), typeof(NavigationItem), new PropertyMetadata(false, OnIsExpandedChanged)); public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(NavigationItem), new PropertyMetadata(false, OnIsSelectedChanged)); public static readonly DependencyProperty SelectionBackgroundProperty = DependencyProperty.Register("SelectionBackground", typeof(Brush), typeof(NavigationItem), new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#0076d7")))); public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(NavigationItemsCollection), typeof(NavigationItem), new PropertyMetadata(null, OnItemsChanged)); private NavigationItem internalNavigationItem; internal NavigationItemsCollection subNavigationItems = new NavigationItemsCollection(); internal NavigationItemsView subItemsControl; private NavigationItemsView popupItemsControl; internal SfNavigationDrawer navigationDrawer; private bool isItemTapped; internal bool isItemSelected; internal Border border; internal Border subItemsBorder; internal Popup popup; private bool isPopupClosed; private Grid subItemsGrid; private double popupHorizontalSpace = 0.9; private double popupVerticalSpace = 3.0; private ToggleButton expander; private bool isSubItemSelected; private Grid headerGrid; internal double IndentationWidth { get { return (double)GetValue(IndentationWidthProperty); } set { SetValue(IndentationWidthProperty, value); } } public DataTemplate IconTemplate { get { return (DataTemplate)GetValue(IconTemplateProperty); } set { SetValue(IconTemplateProperty, value); } } public string IconMemberPath { get { return (string)GetValue(IconMemberPathProperty); } set { SetValue(IconMemberPathProperty, value); } } public DataTemplate ExpanderTemplate { get { return (DataTemplate)GetValue(ExpanderTemplateProperty); } set { SetValue(ExpanderTemplateProperty, value); } } public object Icon { get { return GetValue(IconProperty); } set { SetValue(IconProperty, value); } } public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } public object CommandParameter { get { return GetValue(CommandParameterProperty); } set { SetValue(CommandParameterProperty, value); } } public bool IsChildSelected { get { return (bool)GetValue(IsChildSelectedProperty); } internal set { SetValue(IsChildSelectedProperty, value); } } public ItemType ItemType { get { return (ItemType)GetValue(ItemTypeProperty); } set { SetValue(ItemTypeProperty, value); } } public bool IsExpanded { get { return (bool)GetValue(IsExpandedProperty); } set { SetValue(IsExpandedProperty, value); } } public bool IsSelected { get { return (bool)GetValue(IsSelectedProperty); } set { SetValue(IsSelectedProperty, value); } } public Brush SelectionBackground { get { return (Brush)GetValue(SelectionBackgroundProperty); } set { SetValue(SelectionBackgroundProperty, value); } } public new NavigationItemsCollection Items { get { return (NavigationItemsCollection)GetValue(ItemsProperty); } set { SetValue(ItemsProperty, value); } } public NavigationItem() { Items = new NavigationItemsCollection(); base.DefaultStyleKey = typeof(NavigationItem); } public override void OnApplyTemplate() { subItemsControl = GetTemplateChild("NavigationItemSubItemsItemsControl") as NavigationItemsView; popupItemsControl = GetTemplateChild("NavigationItemPopupItemsControl") as NavigationItemsView; border = GetTemplateChild("NavigationItemHeaderBorder") as Border; subItemsBorder = GetTemplateChild("NavigationItemSubItemsBorder") as Border; headerGrid = GetTemplateChild("NavigationItemHeaderGrid") as Grid; expander = GetTemplateChild("NavigationItemExpander") as ToggleButton; popup = GetTemplateChild("NavigationItemSubItemsPopup") as Popup; subItemsGrid = GetTemplateChild("NavigationItemSubItemsGrid") as Grid; if (base.DataContext == null && navigationDrawer != null) { base.DataContext = navigationDrawer.DataContext; } if (subItemsControl != null) { if (IsExpanded) { subItemsControl.UpdateExpandedAnimation(); } else { subItemsControl.UpdateCollapsedAnimation(); } } if (headerGrid != null && navigationDrawer != null && navigationDrawer.IsOpen) { headerGrid.Margin = new Thickness(IndentationWidth, 0.0, 0.0, 0.0); } if (IsSelected && navigationDrawer != null && navigationDrawer.SelectedItem == null) { navigationDrawer.SelectedItem = this; } if (subNavigationItems != null && subNavigationItems != null) { ((INotifyCollectionChanged)subNavigationItems).CollectionChanged -= NavigationItem_ItemsSource_CollectionChanged; ((INotifyCollectionChanged)subNavigationItems).CollectionChanged += NavigationItem_ItemsSource_CollectionChanged; } if (subItemsGrid != null) { subItemsGrid.SizeChanged -= SubItemsGrid_SizeChanged; subItemsGrid.SizeChanged += SubItemsGrid_SizeChanged; } if (expander != null) { expander.Checked -= Expander_Checked; expander.Checked += Expander_Checked; expander.Unchecked -= Expander_Unchecked; expander.Unchecked += Expander_Unchecked; } if (popup != null) { popup.Closed -= Popup_Closed; popup.Closed += Popup_Closed; } if (border != null) { border.MouseUp -= Border_MouseUp; border.MouseUp += Border_MouseUp; border.MouseDown -= Border_MouseDown; border.MouseDown += Border_MouseDown; } UpdateItemsSource(); base.OnApplyTemplate(); if (IsExpanded && navigationDrawer != null && navigationDrawer.IsOpen) { RaiseItemExpandedEvent(navigationDrawer, this); } } private void Border_MouseDown(object sender, MouseButtonEventArgs e) { isPopupClosed = false; } private void Popup_Closed(object sender, EventArgs e) { isPopupClosed = true; } private void SubItemsGrid_SizeChanged(object sender, SizeChangedEventArgs e) { if (popup != null && navigationDrawer != null) { popup.PlacementRectangle = new Rect(navigationDrawer.CompactModeWidth, 0.0, e.NewSize.Width, 0.0); } } private void Expander_Unchecked(object sender, RoutedEventArgs e) { if (navigationDrawer == null) { return; } if (navigationDrawer.IsOpen) { if (!isItemTapped && ItemType == ItemType.Tab) { RaiseItemClickedEvent(navigationDrawer, this); } else { isItemTapped = false; } RaiseItemCollapsedEvent(navigationDrawer, this); } else if (!navigationDrawer.IsOpen && !navigationDrawer.isItemCollapsed) { navigationDrawer.isItemCollapsed = true; RaiseItemCollapsedEvent(navigationDrawer, this); } } private void Expander_Checked(object sender, RoutedEventArgs e) { if (navigationDrawer != null && navigationDrawer.IsOpen) { if (!isItemTapped && ItemType == ItemType.Tab) { RaiseItemClickedEvent(navigationDrawer, this); } else { isItemTapped = false; } RaiseItemExpandedEvent(navigationDrawer, this); } if (!IsChildSelected) { return; } if (Items != null && Items.Count > 0) { foreach (NavigationItem item in Items) { if (item.Header == internalNavigationItem.Header && !item.IsSelected) { item.IsSelected = true; } } } else if (subNavigationItems != null && subNavigationItems.Count > 0) { foreach (NavigationItem subNavigationItem in subNavigationItems) { if (subNavigationItem.Header == internalNavigationItem.Header && !subNavigationItem.IsSelected) { subNavigationItem.IsSelected = true; } } } IsSelected = false; } internal void RaiseItemClickedEvent(SfNavigationDrawer navigationDrawer, NavigationItem navigationItem) { NavigationItemClickedEventArgs navigationItemClickedEventArgs = new NavigationItemClickedEventArgs(); navigationItemClickedEventArgs.Item = navigationItem; navigationDrawer.RaiseItemClickedEvent(navigationItemClickedEventArgs); } private void RaiseItemCollapsedEvent(SfNavigationDrawer navigationDrawer, NavigationItem navigationItem) { NavigationItemCollapsedEventArgs navigationItemCollapsedEventArgs = new NavigationItemCollapsedEventArgs(); navigationItemCollapsedEventArgs.Item = navigationItem; navigationDrawer.RaiseItemCollapsedEvent(navigationItemCollapsedEventArgs); } private void RaiseItemExpandedEvent(SfNavigationDrawer navigationDrawer, NavigationItem navigationItem) { NavigationItemExpandedEventArgs navigationItemExpandedEventArgs = new NavigationItemExpandedEventArgs(); navigationItemExpandedEventArgs.Item = navigationItem; navigationDrawer.RaiseItemExpandedEvent(navigationItemExpandedEventArgs); } private void Border_MouseUp(object sender, MouseButtonEventArgs e) { isItemSelected = true; isItemTapped = true; if (navigationDrawer != null) { RaiseItemClickedEvent(navigationDrawer, this); } if (IsSelected) { IsSelected = false; } else { IsSelected = true; } } private static void OnIndentationWidthPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { NavigationItem navigationItem = (NavigationItem)d; if (navigationItem != null && navigationItem.headerGrid != null) { navigationItem.headerGrid.Margin = new Thickness(navigationItem.IndentationWidth, 0.0, 0.0, 0.0); } } private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { NavigationItem navigationItem = d as NavigationItem; if (navigationItem != null && navigationItem.ItemsSource != null && navigationItem.subItemsControl != null) { navigationItem.UpdateItemsSource(); } } private static void OnItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { NavigationItem navigationItem = (NavigationItem)d; if (navigationItem != null && !object.Equals(e.NewValue, e.OldValue) && navigationItem.Items != null) { if (navigationItem.navigationDrawer != null) { navigationItem.navigationDrawer.UpdateItems(navigationItem.Items); navigationItem.navigationDrawer.UpdateIndentationWidth(navigationItem.Items, (int)(navigationItem.IndentationWidth / navigationItem.navigationDrawer.IndentationWidth) + navigationItem.navigationDrawer.navigationItemLevel); } if (navigationItem.Items != null) { ((INotifyCollectionChanged)navigationItem.Items).CollectionChanged -= navigationItem.NavigationItem_CollectionChanged; ((INotifyCollectionChanged)navigationItem.Items).CollectionChanged += navigationItem.NavigationItem_CollectionChanged; } } } private void NavigationItem_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { navigationDrawer.UpdateItems(Items); navigationDrawer.UpdateIndentationWidth(Items, (int)(IndentationWidth / navigationDrawer.IndentationWidth) + navigationDrawer.navigationItemLevel); } private void NavigationItem_ItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (navigationDrawer != null) { navigationDrawer.UpdateIndentationWidth(subNavigationItems, (int)(IndentationWidth / navigationDrawer.IndentationWidth) + navigationDrawer.navigationItemLevel); } } private static void OnIsExpandedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { NavigationItem navigationItem = d as NavigationItem; if (navigationItem == null) { return; } if (e.NewValue.ToString() == "True") { if (navigationItem.Items.Count > 0) { foreach (NavigationItem item in navigationItem.Items) { item.Focusable = true; } } else if (navigationItem.subNavigationItems.Count > 0) { foreach (NavigationItem subNavigationItem in navigationItem.subNavigationItems) { subNavigationItem.Focusable = true; } } if (navigationItem.subItemsControl != null) { navigationItem.subItemsControl.UpdateExpandedAnimation(); } } else if (navigationItem.subItemsControl != null) { navigationItem.subItemsControl.UpdateCollapsedAnimation(); } if (navigationItem.navigationDrawer != null && navigationItem.navigationDrawer.IsOpen && e.NewValue.ToString() == "True" && navigationItem.subItemsControl != null) { if (navigationItem.ItemsSource != null) { foreach (NavigationItem subNavigationItem2 in navigationItem.subNavigationItems) { if (subNavigationItem2.IsSelected) { navigationItem.internalNavigationItem = subNavigationItem2; } } navigationItem.UpdateSubItemsIndentationWidth(navigationItem.subNavigationItems); navigationItem.UpdateItemsSource(); } else { navigationItem.subItemsControl.ItemsSource = new NavigationItemsCollection(); if (navigationItem.Items.Count > 0) { foreach (NavigationItem item2 in navigationItem.Items) { if (item2.IsSelected) { navigationItem.internalNavigationItem = item2; } } navigationItem.UpdateSubItemsIndentationWidth(navigationItem.Items); navigationItem.subItemsControl.ItemsSource = navigationItem.Items; } else if (navigationItem.subNavigationItems.Count > 0) { foreach (NavigationItem subNavigationItem3 in navigationItem.subNavigationItems) { if (subNavigationItem3.IsSelected) { navigationItem.internalNavigationItem = subNavigationItem3; } } navigationItem.UpdateSubItemsIndentationWidth(navigationItem.subNavigationItems); navigationItem.subItemsControl.ItemsSource = navigationItem.subNavigationItems; } } } if (e.NewValue.ToString() == "False" && navigationItem.navigationDrawer.IsOpen && !navigationItem.IsSelected && navigationItem.IsChildSelected) //null exception in navigationItem.navigationDrawer when using ItemsSource from ViewModel { navigationItem.navigationDrawer.isExpanderClosed = true; } if (!(e.NewValue.ToString() == "False")) { return; } if (navigationItem.Items.Count > 0) { foreach (NavigationItem item3 in navigationItem.Items) { item3.Focusable = false; } } else if (navigationItem.subNavigationItems.Count > 0) { foreach (NavigationItem subNavigationItem4 in navigationItem.subNavigationItems) { subNavigationItem4.Focusable = false; } } if (navigationItem.IsSelected) { return; } if (navigationItem.Items != null && navigationItem.Items.Count > 0) { foreach (NavigationItem item4 in navigationItem.Items) { if (item4.IsSelected) { navigationItem.isSubItemSelected = true; navigationItem.IsSelected = true; } } } else { if (navigationItem.subNavigationItems == null || navigationItem.subNavigationItems.Count <= 0) { return; } foreach (NavigationItem subNavigationItem5 in navigationItem.subNavigationItems) { if (subNavigationItem5.IsSelected) { navigationItem.isSubItemSelected = true; navigationItem.IsSelected = true; } } } } private static void OnIsSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { NavigationItem navigationItem = d as NavigationItem; if (navigationItem == null || navigationItem.navigationDrawer == null) { return; } if (e.NewValue.ToString() == "True") { if (navigationItem.popup != null && (navigationItem.Items.Count > 0 || navigationItem.subNavigationItems.Count > 0) && !navigationItem.navigationDrawer.IsOpen && !navigationItem.popup.IsOpen && !navigationItem.isPopupClosed && !navigationItem.IsChildSelected) { Thread.Sleep(100); if (navigationItem.popupItemsControl != null) { if (navigationItem.Items.Count > 0) { foreach (NavigationItem item in navigationItem.Items) { item.Focusable = true; } } else if (navigationItem.subNavigationItems.Count > 0) { foreach (NavigationItem subNavigationItem in navigationItem.subNavigationItems) { subNavigationItem.Focusable = true; } } if (navigationItem.ItemsSource != null) { navigationItem.UpdatePopupItemIndentationWidth(navigationItem.subNavigationItems); navigationItem.popupItemsControl.navigationItem = navigationItem; navigationItem.popupItemsControl.DisplayMemberPath = navigationItem.DisplayMemberPath; navigationItem.popupItemsControl.ItemsSource = navigationItem.ItemsSource; } else { navigationItem.popupItemsControl.ItemsSource = new NavigationItemsCollection(); if (navigationItem.Items.Count > 0) { navigationItem.UpdatePopupItemIndentationWidth(navigationItem.Items); navigationItem.popupItemsControl.ItemsSource = navigationItem.Items; } else if (navigationItem.subNavigationItems.Count > 0) { navigationItem.UpdatePopupItemIndentationWidth(navigationItem.subNavigationItems); navigationItem.popupItemsControl.ItemsSource = navigationItem.subNavigationItems; } } } navigationItem.popup.IsOpen = true; } if ((navigationItem.Items != null && navigationItem.Items.Count > 0) || navigationItem.ItemsSource != null) { if (navigationItem.IsExpanded) { navigationItem.IsExpanded = false; } else if (!navigationItem.isSubItemSelected && navigationItem.navigationDrawer.IsOpen) { navigationItem.IsExpanded = true; } else { navigationItem.isSubItemSelected = false; } } if (navigationItem.ItemType == ItemType.Tab) { navigationItem.navigationDrawer.UpdateSelectedItem(navigationItem); } } else if ((navigationItem.Equals(navigationItem.navigationDrawer.SelectedItem) || (navigationItem.DataContext != null && navigationItem.DataContext.Equals(navigationItem.navigationDrawer.SelectedItem)) || (navigationItem.IsChildSelected && !navigationItem.IsExpanded)) && navigationItem.isItemSelected) { navigationItem.IsChildSelected = false; navigationItem.IsSelected = true; } else if (navigationItem.popup != null) { navigationItem.popup.IsOpen = false; navigationItem.popup.Visibility = Visibility.Collapsed; } } private void UpdateItemsSource() { if (subItemsControl != null && base.ItemsSource != null) { subItemsControl.navigationItem = this; subItemsControl.DisplayMemberPath = base.DisplayMemberPath; subItemsControl.ItemsSource = base.ItemsSource; } } private void UpdatePopupItemIndentationWidth(NavigationItemsCollection items) { if (items == null || items.Count <= 0) { return; } foreach (NavigationItem item in items) { item.headerGrid.Margin = new Thickness(0.0, 0.0, 0.0, 0.0); if (item.Items != null && item.Items.Count > 0) { UpdateSubItemsIndentationWidth(item.Items); } else if (item.subNavigationItems != null && item.subNavigationItems.Count > 0) { UpdateSubItemsIndentationWidth(item.subNavigationItems); } } } private void UpdateSubItemsIndentationWidth(NavigationItemsCollection items) { if (items == null || items.Count <= 0) { return; } foreach (NavigationItem item in items) { item.headerGrid.Margin = new Thickness(item.IndentationWidth, 0.0, 0.0, 0.0); if (item.Items != null && item.Items.Count > 0) { UpdateSubItemsIndentationWidth(item.Items); } else if (item.subNavigationItems != null && item.subNavigationItems.Count > 0) { UpdateSubItemsIndentationWidth(item.subNavigationItems); } } } public void Dispose() { Dispose(disposing: true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposing) { if (expander != null) { expander.Checked -= Expander_Checked; expander.Unchecked -= Expander_Unchecked; expander = null; } if (border != null) { border.MouseUp -= Border_MouseUp; border = null; } if (subNavigationItems != null && subNavigationItems != null) { ((INotifyCollectionChanged)subNavigationItems).CollectionChanged -= NavigationItem_ItemsSource_CollectionChanged; } } } } }