Skip to content

Commit c338f12

Browse files
authored
Fixing issue with ComboBoxes in dialog host (#3451)
* Fixing issue with ComboBoxes in dialog host This revert the fix that was done for #3249. Need to revisit fix that was done and find an alternative. * Only calling set focus one when dialog is opened.
1 parent 223e195 commit c338f12

File tree

5 files changed

+97
-20
lines changed

5 files changed

+97
-20
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<UserControl x:Class="MaterialDesignThemes.UITests.Samples.DialogHost.WithComboBox"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:local="clr-namespace:MaterialDesignThemes.UITests.Samples.DialogHost"
8+
mc:Ignorable="d"
9+
d:DesignHeight="450" d:DesignWidth="800">
10+
11+
<materialDesign:DialogHost x:Name="SampleDialogHost"
12+
Loaded="DialogHost_Loaded">
13+
<materialDesign:DialogHost.DialogContent>
14+
<Grid Margin="16">
15+
<Grid.RowDefinitions>
16+
<RowDefinition />
17+
<RowDefinition />
18+
<RowDefinition />
19+
</Grid.RowDefinitions>
20+
<TextBlock Text="Please choose the desired targeted platform "
21+
Margin="6"
22+
FontSize="12"
23+
TextWrapping="Wrap"/>
24+
<ComboBox
25+
x:Name="TargetedPlatformComboBox"
26+
Grid.Row="1"
27+
Width="120"
28+
materialDesign:HintAssist.HelperText="Targetted Platform"
29+
Style="{StaticResource MaterialDesignFloatingHintComboBox}">
30+
<ComboBoxItem Content="Item1"/>
31+
<ComboBoxItem Content="Item2" x:Name="TargetItem"/>
32+
<ComboBoxItem Content="Item3"/>
33+
</ComboBox>
34+
35+
</Grid>
36+
</materialDesign:DialogHost.DialogContent>
37+
</materialDesign:DialogHost>
38+
</UserControl>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace MaterialDesignThemes.UITests.Samples.DialogHost;
2+
3+
/// <summary>
4+
/// Interaction logic for WithComboBox.xaml
5+
/// </summary>
6+
public partial class WithComboBox : UserControl
7+
{
8+
public WithComboBox()
9+
{
10+
InitializeComponent();
11+
}
12+
13+
private void DialogHost_Loaded(object sender, RoutedEventArgs e)
14+
{
15+
SampleDialogHost.IsOpen = true;
16+
}
17+
}

MaterialDesignThemes.UITests/WPF/ComboBoxes/ComboBoxTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public async Task OnEditableComboBox_ClickInTextArea_FocusesTextBox()
145145
var editableTextBox = await comboBox.GetElement<TextBox>("PART_EditableTextBox");
146146
var button = await stackPanel.GetElement<Button>("Button");
147147

148-
// Open the combobox initially
148+
// Open the ComboBox initially
149149
await comboBox.LeftClick(Position.RightCenter);
150150
await Task.Delay(50); // Allow a little time for the drop-down to open (and property to change)
151151
bool wasOpenAfterClickOnToggleButton = await comboBox.GetIsDropDownOpen();

MaterialDesignThemes.UITests/WPF/DialogHosts/DialogHostTests.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@
66

77
namespace MaterialDesignThemes.UITests.WPF.DialogHosts;
88

9-
public class DialogHostTests : TestBase
9+
public class DialogHostTests(ITestOutputHelper output) : TestBase(output)
1010
{
11-
public DialogHostTests(ITestOutputHelper output) : base(output)
12-
{
13-
}
14-
1511
[Fact]
1612
public async Task OnOpenDialog_OverlayCoversContent()
1713
{
@@ -478,6 +474,32 @@ public async Task DialogHost_ChangesSelectedRailItem_DoesNotPerformRailChangeWhe
478474
Assert.True(await railItem1.GetIsSelected());
479475
Assert.False(await railItem2.GetIsSelected());
480476

477+
recorder.Success();
478+
}
479+
480+
[Fact]
481+
[Description("Issue 3450")]
482+
public async Task DialogHost_WithComboBox_CanSelectItem()
483+
{
484+
await using var recorder = new TestRecorder(App);
485+
486+
IVisualElement dialogHost = await LoadUserControl<WithComboBox>();
487+
488+
var comboBox = await dialogHost.GetElement<ComboBox>("TargetedPlatformComboBox");
489+
await Task.Delay(500);
490+
await comboBox.LeftClick();
491+
492+
var item = await Wait.For(() => comboBox.GetElement<ComboBoxItem>("TargetItem"));
493+
await Task.Delay(TimeSpan.FromSeconds(1));
494+
await item.LeftClick();
495+
496+
await Wait.For(async () =>
497+
{
498+
var index = await comboBox.GetSelectedIndex();
499+
Assert.Equal(1, index);
500+
});
501+
502+
481503
recorder.Success();
482504
}
483505
}

MaterialDesignThemes.Wpf/DialogHost.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@ public DialogHost()
287287
{
288288
Loaded += OnLoaded;
289289
Unloaded += OnUnloaded;
290-
PreviewGotKeyboardFocus += OnPreviewGotKeyboardFocus;
291290

292291
CommandBindings.Add(new CommandBinding(CloseDialogCommand, CloseDialogHandler, CloseDialogCanExecute));
293292
CommandBindings.Add(new CommandBinding(OpenDialogCommand, OpenDialogHandler));
@@ -433,8 +432,8 @@ public PlacementMode Placement
433432

434433
public CornerRadius CornerRadius
435434
{
436-
get { return (CornerRadius)GetValue(CornerRadiusProperty); }
437-
set { SetValue(CornerRadiusProperty, value); }
435+
get => (CornerRadius)GetValue(CornerRadiusProperty);
436+
set => SetValue(CornerRadiusProperty, value);
438437
}
439438

440439
public static readonly DependencyProperty DialogContentProperty = DependencyProperty.Register(
@@ -652,8 +651,8 @@ public bool IsRestoreFocusDisabled
652651
/// </summary>
653652
public event DialogOpenedEventHandler DialogOpened
654653
{
655-
add { AddHandler(DialogOpenedEvent, value); }
656-
remove { RemoveHandler(DialogOpenedEvent, value); }
654+
add => AddHandler(DialogOpenedEvent, value);
655+
remove => RemoveHandler(DialogOpenedEvent, value);
657656
}
658657

659658
/// <summary>
@@ -699,8 +698,8 @@ protected void OnDialogOpened(DialogOpenedEventArgs eventArgs)
699698
/// </summary>
700699
public event DialogClosingEventHandler DialogClosing
701700
{
702-
add { AddHandler(DialogClosingEvent, value); }
703-
remove { RemoveHandler(DialogClosingEvent, value); }
701+
add => AddHandler(DialogClosingEvent, value);
702+
remove => RemoveHandler(DialogClosingEvent, value);
704703
}
705704

706705
/// <summary>
@@ -742,8 +741,8 @@ protected void OnDialogClosing(DialogClosingEventArgs eventArgs)
742741
/// </summary>
743742
public event DialogClosedEventHandler DialogClosed
744743
{
745-
add { AddHandler(DialogClosedEvent, value); }
746-
remove { RemoveHandler(DialogClosedEvent, value); }
744+
add => AddHandler(DialogClosedEvent, value);
745+
remove => RemoveHandler(DialogClosedEvent, value);
747746
}
748747

749748
/// <summary>
@@ -819,6 +818,11 @@ internal void InternalClose(object? parameter)
819818
var child = _popup?.Child ?? _popupContentControl;
820819
if (child is null) return null;
821820

821+
if (PresentationSource.FromVisual(child) is HwndSource hwndSource)
822+
{
823+
SetFocus(hwndSource.Handle);
824+
}
825+
822826
CommandManager.InvalidateRequerySuggested();
823827
var focusable = child.VisualDepthFirstTraversal().OfType<UIElement>().FirstOrDefault(ui => ui.Focusable);
824828
if (focusable is null) return null;
@@ -954,11 +958,7 @@ private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
954958

955959
private void OnPreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
956960
{
957-
if (_popup != null &&
958-
PresentationSource.FromVisual(_popup.Child) is HwndSource hwndSource)
959-
{
960-
SetFocus(hwndSource.Handle);
961-
}
961+
962962
}
963963

964964
[SecurityCritical]

0 commit comments

Comments
 (0)