From 20cac259fe6e215a17852d1d2dc8e61c19caf66d Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Tue, 15 Mar 2022 16:27:51 -0700 Subject: [PATCH] fix: Workaround XAML bug with ComboBox and dark theme (#380) --- windows/ReactNativePicker/ReactPickerView.cpp | 15 +++++++++++++++ windows/ReactNativePicker/ReactPickerView.h | 1 + 2 files changed, 16 insertions(+) diff --git a/windows/ReactNativePicker/ReactPickerView.cpp b/windows/ReactNativePicker/ReactPickerView.cpp index 0716c48a54..b85810890b 100644 --- a/windows/ReactNativePicker/ReactPickerView.cpp +++ b/windows/ReactNativePicker/ReactPickerView.cpp @@ -8,6 +8,7 @@ #include #include +#include namespace winrt { using namespace Microsoft::ReactNative; @@ -50,6 +51,20 @@ namespace winrt::ReactNativePicker::implementation { winrt::FocusManager::TryFocusAsync(comboBox, winrt::FocusState::Programmatic); } }); + + // Workaround XAML bug with ComboBox and dark theme. Same as: + // https://github.com/microsoft/microsoft-ui-xaml/issues/2331 + m_dropDownOpenedRevoker = this->DropDownOpened(winrt::auto_revoke, + [](auto const& sender, auto const& /*args*/) { + auto comboBox = sender.as(); + if (comboBox.XamlRoot()) { // XamlRoot added in 19H1 + auto comboBoxAsFrameworkElement = comboBox.XamlRoot().Content().try_as(); + auto popups = xaml::Media::VisualTreeHelper::GetOpenPopupsForXamlRoot(comboBox.XamlRoot()); + for (auto const& popup : popups) { + popup.Child().as().RequestedTheme(comboBoxAsFrameworkElement.ActualTheme()); + } + } + }); } void ReactPickerView::UpdateProperties(winrt::IJSValueReader const& reader) { diff --git a/windows/ReactNativePicker/ReactPickerView.h b/windows/ReactNativePicker/ReactPickerView.h index 96c19fa130..c8728f9254 100644 --- a/windows/ReactNativePicker/ReactPickerView.h +++ b/windows/ReactNativePicker/ReactPickerView.h @@ -25,6 +25,7 @@ namespace winrt::ReactNativePicker::implementation { xaml::Media::Brush m_comboBoxColor{ nullptr }; xaml::Controls::ComboBox::SelectionChanged_revoker m_selectionChangedRevoker{}; xaml::Controls::ComboBox::DropDownClosed_revoker m_dropDownClosedRevoker{}; + xaml::Controls::ComboBox::DropDownOpened_revoker m_dropDownOpenedRevoker{}; void RegisterEvents(); void RepopulateItems(winrt::Microsoft::ReactNative::JSValueArray const& items);