Skip to content

Commit

Permalink
chore: Properly display text for selected item
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Aug 2, 2024
1 parent 9253de6 commit 531dd2a
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/ComboBox/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,14 +587,14 @@ private string TryGetStringValue(object @object/*, PropertyPathListener pathList
}
else
{
return spBoxedValue.ToString()!;
return FrameworkElement.GetStringFromObject(spBoxedValue);
// We've set a BoxedValue, but we can't directly ToString it. Try to get a string out of it.
}
}
else
{
// If we haven't found a BoxedObject and it's not Stringable, try one last time to get a string out.
return @object.ToString()!;
return FrameworkElement.GetStringFromObject(@object);
}
}

Expand Down
15 changes: 10 additions & 5 deletions src/Uno.UI/UI/Xaml/Controls/ComboBox/ComboBox.partial.mux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ private void SetContentPresenter(int index, bool forceSelectionBoxToNull = false
}
}

var spGenerator = ItemContainerGenerator;
ItemContainerGenerator pItemContainerGenerator = spGenerator;
if (m_iLastGeneratedItemIndexforFaceplate > 0)
ItemContainerGenerator? spGenerator = null; // TODO Uno: Support for ItemContainerGenerator #17808 (Should reference this.ItemContainerGenerator property)
ItemContainerGenerator? pItemContainerGenerator = spGenerator;
if (m_iLastGeneratedItemIndexforFaceplate > 0 && pItemContainerGenerator is not null)
{
// This container was generated just for the purpose of extracting Content and ContentTemplate.
// This is the case where we generated an item which was its own container (e.g. defined in XAML or code behind).
Expand Down Expand Up @@ -358,7 +358,7 @@ private void SetContentPresenter(int index, bool forceSelectionBoxToNull = false
spContainer = ContainerFromIndex(index);
spComboBoxItem = spContainer as ComboBoxItem;

if (spComboBoxItem is null)
if (spComboBoxItem is null && pItemContainerGenerator is not null)
{
bool isNewlyRealized = false;
generatorPosition = pItemContainerGenerator.GeneratorPositionFromIndex(index);
Expand All @@ -385,6 +385,11 @@ private void SetContentPresenter(int index, bool forceSelectionBoxToNull = false
m_iLastGeneratedItemIndexforFaceplate = index;
}

if (spComboBoxItem is null)
{
return;
}

spContent = spComboBoxItem.Content;
{
// Because we can't keep UIElement in 2 different place
Expand Down Expand Up @@ -421,7 +426,7 @@ private void SetContentPresenter(int index, bool forceSelectionBoxToNull = false

}

if (bGeneratedComboBoxItem)
if (bGeneratedComboBoxItem && pItemContainerGenerator is not null)
{
// This container was generated just for the purpose of extracting Content and ContentTemplate
// It is not connected to the visual tree which might have unintended consequences, so remove it
Expand Down
10 changes: 10 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/ContentPresenter/ContentPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1413,4 +1413,14 @@ private void SetUpdateTemplate()
}

partial void SetUpdateTemplatePartial();

internal string GetTextBlockText()
{
if (IsUsingDefaultTemplate && ContentTemplateRoot is ImplicitTextBlock tb)
{
return tb.Text;
}

return null;
}
}
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Controls/MenuFlyout/MenuFlyoutItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ protected override AutomationPeer OnCreateAutomationPeer()
return new MenuFlyoutItemAutomationPeer(this);
}

private protected override string GetPlainText() => Text;
internal override string GetPlainText() => Text;

internal string KeyboardAcceleratorTextOverrideImpl
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ protected override void OnPointerEntered(PointerRoutedEventArgs pArgs)
}
}

private protected override string GetPlainText()
internal override string GetPlainText()
{
string automationName = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ protected override AutomationPeer OnCreateAutomationPeer()
return null;
}

private protected override string GetPlainText() => Text;
internal override string GetPlainText() => Text;

bool ISubMenuOwner.IsSubMenuOpen => IsOpen;

Expand Down
65 changes: 65 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/Primitives/SelectorItem.partial.mux.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.UI.Xaml.Automation;
using static Uno.UI.FeatureConfiguration;

namespace Microsoft.UI.Xaml.Controls.Primitives;

partial class SelectorItem
{
//---------------------------------------------------------------------------
//
// Synopsis:
// Returns a plain text string to provide a default AutomationProperties.Name
// in the absence of an explicitly defined one
//
//---------------------------------------------------------------------------
internal override string GetPlainText()
{
string strPlainText = null;

var contentTemplateRoot = ContentTemplateRoot;

if (contentTemplateRoot != null)
{
// we have the first child of the content. Check whether it has an automation name

strPlainText = AutomationProperties.GetName(contentTemplateRoot);

// fallback: use getplain text on it
if (string.IsNullOrEmpty(strPlainText))
{
var contentTemplateRootAsIFE = contentTemplateRoot as FrameworkElement;

strPlainText = null;

if (contentTemplateRootAsIFE is not null)
{
strPlainText = contentTemplateRootAsIFE.GetPlainText();
}
}

// fallback, use GetPlainText on the contentpresenter, who has some special logic to account for old templates
if (string.IsNullOrEmpty(strPlainText))
{
var contentTemplateRootAsIFE = contentTemplateRoot as FrameworkElement;

strPlainText = null;

if (contentTemplateRootAsIFE is not null)
{
var pParent = contentTemplateRootAsIFE.Parent;
if (pParent is ContentPresenter cp)
{
strPlainText = cp.GetTextBlockText();
}
}
}
}

return strPlainText;
}
}
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Controls/Slider/Slider.mux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ private void OnPointerCaptureLost(object sender, PointerRoutedEventArgs args)
// in the absence of an explicitly defined one
//
//---------------------------------------------------------------------------
private protected override string GetPlainText()
internal override string GetPlainText()
{
var header = Header;

Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/FrameworkElement.mux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.UI.Xaml
{
public partial class FrameworkElement
{
private protected virtual string GetPlainText() => "";
internal virtual string GetPlainText() => "";

internal protected static string GetStringFromObject(object pObject)
{
Expand Down

0 comments on commit 531dd2a

Please sign in to comment.