Skip to content

Commit

Permalink
Fixing issue with ComboBoxes in dialog host (#3451)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Keboo committed Feb 13, 2024
1 parent 223e195 commit c338f12
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 20 deletions.
38 changes: 38 additions & 0 deletions MaterialDesignThemes.UITests/Samples/DialogHost/WithComboBox.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<UserControl x:Class="MaterialDesignThemes.UITests.Samples.DialogHost.WithComboBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MaterialDesignThemes.UITests.Samples.DialogHost"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">

<materialDesign:DialogHost x:Name="SampleDialogHost"
Loaded="DialogHost_Loaded">
<materialDesign:DialogHost.DialogContent>
<Grid Margin="16">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Please choose the desired targeted platform "
Margin="6"
FontSize="12"
TextWrapping="Wrap"/>
<ComboBox
x:Name="TargetedPlatformComboBox"
Grid.Row="1"
Width="120"
materialDesign:HintAssist.HelperText="Targetted Platform"
Style="{StaticResource MaterialDesignFloatingHintComboBox}">
<ComboBoxItem Content="Item1"/>
<ComboBoxItem Content="Item2" x:Name="TargetItem"/>
<ComboBoxItem Content="Item3"/>
</ComboBox>

</Grid>
</materialDesign:DialogHost.DialogContent>
</materialDesign:DialogHost>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace MaterialDesignThemes.UITests.Samples.DialogHost;

/// <summary>
/// Interaction logic for WithComboBox.xaml
/// </summary>
public partial class WithComboBox : UserControl
{
public WithComboBox()
{
InitializeComponent();
}

private void DialogHost_Loaded(object sender, RoutedEventArgs e)
{
SampleDialogHost.IsOpen = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public async Task OnEditableComboBox_ClickInTextArea_FocusesTextBox()
var editableTextBox = await comboBox.GetElement<TextBox>("PART_EditableTextBox");
var button = await stackPanel.GetElement<Button>("Button");

// Open the combobox initially
// Open the ComboBox initially
await comboBox.LeftClick(Position.RightCenter);
await Task.Delay(50); // Allow a little time for the drop-down to open (and property to change)
bool wasOpenAfterClickOnToggleButton = await comboBox.GetIsDropDownOpen();
Expand Down
32 changes: 27 additions & 5 deletions MaterialDesignThemes.UITests/WPF/DialogHosts/DialogHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@

namespace MaterialDesignThemes.UITests.WPF.DialogHosts;

public class DialogHostTests : TestBase
public class DialogHostTests(ITestOutputHelper output) : TestBase(output)
{
public DialogHostTests(ITestOutputHelper output) : base(output)
{
}

[Fact]
public async Task OnOpenDialog_OverlayCoversContent()
{
Expand Down Expand Up @@ -478,6 +474,32 @@ public async Task DialogHost_ChangesSelectedRailItem_DoesNotPerformRailChangeWhe
Assert.True(await railItem1.GetIsSelected());
Assert.False(await railItem2.GetIsSelected());

recorder.Success();
}

[Fact]
[Description("Issue 3450")]
public async Task DialogHost_WithComboBox_CanSelectItem()
{
await using var recorder = new TestRecorder(App);

IVisualElement dialogHost = await LoadUserControl<WithComboBox>();

var comboBox = await dialogHost.GetElement<ComboBox>("TargetedPlatformComboBox");
await Task.Delay(500);
await comboBox.LeftClick();

var item = await Wait.For(() => comboBox.GetElement<ComboBoxItem>("TargetItem"));
await Task.Delay(TimeSpan.FromSeconds(1));
await item.LeftClick();

await Wait.For(async () =>
{
var index = await comboBox.GetSelectedIndex();
Assert.Equal(1, index);
});


recorder.Success();
}
}
28 changes: 14 additions & 14 deletions MaterialDesignThemes.Wpf/DialogHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ public DialogHost()
{
Loaded += OnLoaded;
Unloaded += OnUnloaded;
PreviewGotKeyboardFocus += OnPreviewGotKeyboardFocus;

CommandBindings.Add(new CommandBinding(CloseDialogCommand, CloseDialogHandler, CloseDialogCanExecute));
CommandBindings.Add(new CommandBinding(OpenDialogCommand, OpenDialogHandler));
Expand Down Expand Up @@ -433,8 +432,8 @@ public PlacementMode Placement

public CornerRadius CornerRadius
{
get { return (CornerRadius)GetValue(CornerRadiusProperty); }
set { SetValue(CornerRadiusProperty, value); }
get => (CornerRadius)GetValue(CornerRadiusProperty);
set => SetValue(CornerRadiusProperty, value);
}

public static readonly DependencyProperty DialogContentProperty = DependencyProperty.Register(
Expand Down Expand Up @@ -652,8 +651,8 @@ public bool IsRestoreFocusDisabled
/// </summary>
public event DialogOpenedEventHandler DialogOpened
{
add { AddHandler(DialogOpenedEvent, value); }
remove { RemoveHandler(DialogOpenedEvent, value); }
add => AddHandler(DialogOpenedEvent, value);
remove => RemoveHandler(DialogOpenedEvent, value);
}

/// <summary>
Expand Down Expand Up @@ -699,8 +698,8 @@ protected void OnDialogOpened(DialogOpenedEventArgs eventArgs)
/// </summary>
public event DialogClosingEventHandler DialogClosing
{
add { AddHandler(DialogClosingEvent, value); }
remove { RemoveHandler(DialogClosingEvent, value); }
add => AddHandler(DialogClosingEvent, value);
remove => RemoveHandler(DialogClosingEvent, value);
}

/// <summary>
Expand Down Expand Up @@ -742,8 +741,8 @@ protected void OnDialogClosing(DialogClosingEventArgs eventArgs)
/// </summary>
public event DialogClosedEventHandler DialogClosed
{
add { AddHandler(DialogClosedEvent, value); }
remove { RemoveHandler(DialogClosedEvent, value); }
add => AddHandler(DialogClosedEvent, value);
remove => RemoveHandler(DialogClosedEvent, value);
}

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

if (PresentationSource.FromVisual(child) is HwndSource hwndSource)
{
SetFocus(hwndSource.Handle);
}

CommandManager.InvalidateRequerySuggested();
var focusable = child.VisualDepthFirstTraversal().OfType<UIElement>().FirstOrDefault(ui => ui.Focusable);
if (focusable is null) return null;
Expand Down Expand Up @@ -954,11 +958,7 @@ private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)

private void OnPreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (_popup != null &&
PresentationSource.FromVisual(_popup.Child) is HwndSource hwndSource)
{
SetFocus(hwndSource.Handle);
}

}

[SecurityCritical]
Expand Down

0 comments on commit c338f12

Please sign in to comment.