让我们假设这个组合框是MainPage.xaml.
<ComboBox x:Name="themeMode" SelectionChanged="Themes_SelectionChanged">
<ComboBoxItem>Light</ComboBoxItem>
<ComboBoxItem>Dark</ComboBoxItem>
<ComboBoxItem>Defaut</ComboBoxItem>
</ComboBox>
然后在MainPage.xaml.cs分钟内
private void Themes_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is ComboBox comboBox &&
comboBox.SelectedItem is ComboBoxItem comboBoxItem &&
comboBoxItem.Content is string themeString &&
Enum.TryParse(themeString, out ElementTheme theme) is true)
{
this.RequestedTheme = theme;
}
}
此外,您可以这样做,而不是手动在XAML中添加项.
在MainPage.xaml年中:
public ImmutableArray<string> ElementThemeOptions { get; } = ImmutableArray.Create(Enum.GetNames<ElementTheme>());
private void Themes_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is ComboBox comboBox &&
comboBox.SelectedValue is string themeString &&
Enum.TryParse(themeString, out ElementTheme theme) is true)
{
this.RequestedTheme = theme;
}
}
而在MainPage.xaml年:
<ComboBox
ItemsSource="{x:Bind ElementThemeOptions}"
SelectionChanged="Themes_SelectionChanged" />
UPDATE个
不要忘记在根页面中执行此操作,在本例中为MainPage
.
<Window
...
...>
<local:MainPage />
</Window>