Reputation: 55
In WPF, I would typically do something like this to implement a checkable menu item tied to a setting:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<DockPanel
Grid.Row="0"
LastChildFill="True">
<Menu DockPanel.Dock="Left" IsMainMenu="True">
<MenuItem FontSize="16" Header="_☰">
<MenuItem Header="_I Contain a submenu">
<MenuItem
Header="_I am checkable1"
IsCheckable="True"
IsChecked="{Binding Source={x:Static properties:Settings.Default},Path=IsSetting1Enabled}"
StaysOpenOnClick="True" />
<MenuItem
Header="_I am checkable2"
IsCheckable="True"
IsChecked="{Binding Source={x:Static properties:Settings.Default},Path=IsSetting2Enabled}"
StaysOpenOnClick="True" />
</MenuItem>
<!-- more menu content -->
</MenuItem>
</Menu>
<!-- more dockpanel content-->
</DockPanel>
<!-- more grid content-->
</Grid>
Can I do this in .NET MAUI? If so, how?
Settings.cs (since MAUI uses properties instead of settings):
public static class Settings
{
public static bool IsSetting1Enabled
{
get => Preferences.Get(nameof(IsSetting1Enabled), false);
set => SetPreference(IsSetting1Enabled, value, nameof(IsSetting1Enabled));
}
//More settings here
}
My attempt at CheckableMenuFlytoutItem.cs:
internal class CheckableMenuFlyoutItem : MenuFlyoutItem
{
[Bindable(true)]
[Category("Appearance")]
public bool IsChecked { get; set; }
[Bindable(true)]
[Category("Behavior")]
public bool IsCheckable { get; set; }
[Category("Behavior")]
public event EventHandler Checked;
[Category("Behavior")]
public event EventHandler Unchecked;
protected virtual void OnChecked(EventArgs e);
protected virtual void OnUnchecked(EventArgs e);
}
MyContentPage.xaml:
<MenuFlyoutSubItem Text="Date Display (Input)">
<CheckableMenuFlyoutItem
Name="SimpleTimeInputCheckbox"
Text="Simple Time"
IsCheckable="True"
IsChecked="{Binding Source={x:Static properties:Settings.IsSetting1Enabled}" />
But I don't know how to properly implement OnChecked and OnUnchecked, and while I'm trying to adapt the WPF source code at https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/MenuItem.cs,d31d1ae141bfea57, I haven't had much luck yet.
Can anyone help me either by pointing to an existing implementation of a checkable menu item for MAUI, or by getting me started on building my own?
Upvotes: 0
Views: 177
Reputation: 55
It would appear this is not currently available in MAUI, nor has anyone implemented a version of it that I have found. I have submitted a feature request on the MAUI GitHub repository:
https://github.com/dotnet/maui/issues/16635
Upvotes: 0