我有 c# WPF 應用程式,它使用 NavigationView 控制元件來提供選單樹。我想根據應用程式的狀態啟用/禁用選單中的專案。
我的 NavigationView 在 xaml 中宣告如下
MyApp.xaml
<Window.Resources>
<!-- Simple data template for navigation menu items -->
<DataTemplate x:Key="NavigationViewMenuItem">
<ui:NavigationViewItem
Content="{Binding Title}"
Icon="{Binding Icon}"
SelectsOnInvoked="{Binding Selectable}"
FontWeight="{Binding Selectable, Converter={x:Static cnv:FontConverters.FontWeight}}"
MenuItemsSource="{Binding Children}"/>
</DataTemplate>
<SolidColorBrush x:Key="NavigationViewItemForegroundSelected" Color="{StaticResource CoralMagentaLight2}"/>
<SolidColorBrush x:Key="NavigationViewItemForegroundSelectedPointerOver" Color="{StaticResource CoralMagentaLight2}"/>
<SolidColorBrush x:Key="NavigationViewItemForegroundSelectedPressed" Color="{StaticResource CoralMagentaLight2}"/>
</Window.Resources>
<ui:NavigationView
x:Name="NavigationView"
IsBackButtonVisible="Collapsed"
MenuItemTemplate="{StaticResource NavigationViewMenuItem}"
PaneDisplayMode="Auto"
md:ShadowAssist.ShadowEdges="Right">
控制元件系結到視圖模型上的串列
MyApp.xaml.cs
this.OneWayBind(ViewModel, vm => vm.MenuItems, v => v.NavigationView.MenuItemsSource)
.DisposeWith(disposables);
MyViewModel.cs
[Reactive]
public List<MenuItemModel> MenuItems { get; set; } = NavigationHelper.NavigationHierarchy;
我提供了一種方法來更改給定專案的可選狀態,如下所示
public void SetMenuItemState(string title, bool enabled)
{
var item = MenuItems.Where(x => x.Title == title).First();
if (item != null)
{
if (item.Selectable != enabled)
{
item.Selectable = enabled;
// Required to completely re-assign the MenuItems for
// the change to take affect.
MenuItems = NavigationHelper.EmptyHierarchy;
MenuItems = items;
}
}
}
我想避免必須更新整個串列才能使更改生效,并通過就地更改選單項狀態,如下所示,但這不起作用。
public void SetMenuItemState(string title, bool enabled)
{
var items = MenuItems;
var item = MenuItems.Where(x => x.Title == title).First();
if (item != null)
{
int index = MenuItems.IndexOf(item);
MenuItems[index].Selectable = enabled;
}
}
我嘗試制作選單項串列和可觀察的集合,但這沒有任何區別。如果可能的話,如果有人可以建議一種禁用/啟用選單項而不必重新分配整個串列的方法,我將不勝感激。
uj5u.com熱心網友回復:
您MenuItemModel應該為該屬性實施INotifyPropertyChanged并引發事件:PropertyChangedSelectable
public class MenuItemModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int _selectable;
public bool Selectable
{
get { return _selectable; }
set { _selectable = value; NotifyPropertyChanged(); }
}
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
...
}
然后你的原始代碼應該可以作業。沒有理由重新設定menuItems[index]或切換到ObservableCollection<T>.
uj5u.com熱心網友回復:
解決方法是改變
List<MenuItemModel>
到
ObservableCollection<MenuItemModel>
并實作對 Selectable 屬性的更改,如下所示。
static public void SetMenuItemState(ref ObservableCollection<MenuItemModel> menuItems, string title, bool enabled)
{
var item = menuItems.Where(x => x.Title == title).FirstOrDefault();
if (item != null)
{
int index = menuItems.IndexOf(item);
if (item.Selectable != enabled)
{
item.Selectable = enabled;
menuItems[index] = item;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/425997.html
