-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Settings] Fix flyout position when ComboBox is aligned to the right #31
Merged
natsurainko
merged 7 commits into
Xcube-Studio:winui3-edition
from
gaviny82:fix-combobox-right-alignment
Feb 19, 2023
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ab76f24
Mitigate flyout position issue when ComboBox is aligned to the right
gaviny82 d6bea84
Initial work on calculating ComboBox width based on its content
gaviny82 df73bf2
Determine Width of ComboBox using ItemContainerGenerator
gaviny82 35e43b7
Fix event registration to ensure correct behavior when SetComboBoxWid…
gaviny82 f11b974
Fix appearance issue when there is no content
gaviny82 405d8c5
Bind MaxWidth to a grid column
gaviny82 676e250
Remove width setting for debug
gaviny82 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
Natsurainko.FluentLauncher/Behaviors/SetComboBoxWidthFromItemsBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows.Threading; | ||
using Microsoft.UI.Dispatching; | ||
using Microsoft.UI.Xaml; | ||
using Microsoft.UI.Xaml.Automation.Peers; | ||
using Microsoft.UI.Xaml.Automation.Provider; | ||
using Microsoft.UI.Xaml.Controls; | ||
using Microsoft.UI.Xaml.Controls.Primitives; | ||
using Microsoft.Xaml.Interactivity; | ||
using Windows.Foundation; | ||
|
||
namespace Natsurainko.FluentLauncher.Behaviors | ||
{ | ||
class SetComboBoxWidthFromItemsBehavior : Behavior<ComboBox> | ||
{ | ||
public static readonly DependencyProperty SetComboBoxWidthFromItemsProperty = | ||
DependencyProperty.RegisterAttached | ||
( | ||
"SetComboBoxWidthFromItems", | ||
typeof(bool), | ||
typeof(SetComboBoxWidthFromItemsBehavior), | ||
new PropertyMetadata(false, OnSetComboBoxWidthFromItemsPropertyChanged) | ||
); | ||
|
||
public bool SetComboBoxWidthFromItems | ||
{ | ||
get => (bool)GetValue(SetComboBoxWidthFromItemsProperty); | ||
set => SetValue(SetComboBoxWidthFromItemsProperty, value); | ||
} | ||
|
||
protected override void OnAttached() | ||
{ | ||
AssociatedObject.Loaded += OnComboBoxLoaded; | ||
} | ||
|
||
private static void OnSetComboBoxWidthFromItemsPropertyChanged( | ||
DependencyObject dpo, | ||
DependencyPropertyChangedEventArgs e) | ||
{ | ||
ComboBox comboBox = ((SetComboBoxWidthFromItemsBehavior)dpo).AssociatedObject; | ||
bool newValue = (bool)e.NewValue; | ||
bool oldValue = (bool)e.OldValue; | ||
|
||
if (comboBox != null && newValue != oldValue) | ||
{ | ||
if (newValue == true) | ||
{ | ||
comboBox.Loaded += OnComboBoxLoaded; | ||
} | ||
else | ||
{ | ||
comboBox.Loaded -= OnComboBoxLoaded; | ||
} | ||
} | ||
} | ||
|
||
private static void OnComboBoxLoaded(object sender, RoutedEventArgs e) | ||
{ | ||
ComboBox comboBox = (ComboBox) sender; | ||
|
||
comboBox.DispatcherQueue.TryEnqueue(() => { comboBox.SetWidthFromItems(); }); | ||
} | ||
} | ||
|
||
public static class ComboBoxExtensionMethods | ||
{ | ||
public static void SetWidthFromItems(this ComboBox comboBox) | ||
{ | ||
double comboBoxWidth = 60; // size of combobox without content | ||
|
||
// Create the peer and provider to expand the comboBox in code behind. | ||
ComboBoxAutomationPeer peer = new ComboBoxAutomationPeer(comboBox); | ||
IExpandCollapseProvider provider = (IExpandCollapseProvider)peer.GetPattern(PatternInterface.ExpandCollapse); | ||
|
||
EventHandler<object> eventHandler = null; | ||
eventHandler = new EventHandler<object>((_, _) => | ||
{ | ||
if (comboBox.IsDropDownOpen //&& | ||
/*comboBox.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated*/) | ||
{ | ||
double width = 0; | ||
|
||
// Get the container of the item | ||
foreach (var item in comboBox.Items) | ||
{ | ||
//TODO: combobox.items are not necessarily ComboBoxItems | ||
// var cont = comboBox.ContainerFromIndex(0); | ||
// var a = comboBox.ItemContainerGenerator.ContainerFromItem(item); | ||
TextBlock comboBoxItem = new TextBlock { Text = item.ToString() }; | ||
comboBoxItem.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); | ||
if (comboBoxItem.DesiredSize.Width > width) | ||
{ | ||
width = comboBoxItem.DesiredSize.Width; | ||
} | ||
} | ||
comboBox.Width = comboBoxWidth + width; | ||
// Remove the event handler. | ||
// comboBox.ItemContainerGenerator.StatusChanged -= eventHandler; | ||
comboBox.DropDownOpened -= eventHandler; | ||
provider.Collapse(); | ||
} | ||
}); | ||
|
||
// comboBox.ItemContainerGenerator.StatusChanged += eventHandler; | ||
comboBox.DropDownOpened += eventHandler; | ||
|
||
// Expand the comboBox to generate all its ComboBoxItem's. | ||
provider.Expand(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
本来应该用ItemContainerGenerator获取容器宽度的,但是这一组API好像已经弃用了(ItemContainerGenerator Class | MS Learn),使用
GenerateNext
也总是返回null
,不确定是不是控件加载顺序的问题。现在的方法是new TextBlock然后获取DesiredSize加上一个固定的宽度,对于string类型的ComboBoxItem没问题,如果设置了ItemTemplate就不能用。