Skip to content
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

Add MetadataControl #3686

Merged
8 commits merged into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public sealed partial class MetadataControlPage : Page, IXamlRenderListener
private static readonly string[] Labels = "Lorem ipsum dolor sit amet consectetur adipiscing elit".Split(" ");

private readonly Random _random;
private readonly ObservableCollection<MetadataUnit> _units;
private readonly ObservableCollection<MetadataItem> _units;
private readonly DelegateCommand<object> _command;
private MetadataControl _metadataControl;

public MetadataControlPage()
{
_random = new Random();
_units = new ObservableCollection<MetadataUnit>();
_units = new ObservableCollection<MetadataItem>();
_command = new DelegateCommand<object>(OnExecuteCommand);
InitializeComponent();
Setup();
Expand All @@ -38,21 +38,21 @@ public void OnXamlRendered(FrameworkElement control)
_metadataControl = control.FindChildByName("metadataControl") as MetadataControl;
if (_metadataControl != null)
{
_metadataControl.MetadataUnits = _units;
_metadataControl.Items = _units;
}
}

private void Setup()
{
SampleController.Current.RegisterNewCommand("Add label", (sender, args) =>
{
_units.Add(new MetadataUnit { Label = GetRandomLabel() });
_units.Add(new MetadataItem { Label = GetRandomLabel() });
});

SampleController.Current.RegisterNewCommand("Add command", (sender, args) =>
{
var label = GetRandomLabel();
_units.Add(new MetadataUnit
_units.Add(new MetadataItem
{
Label = label,
Command = _command,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Microsoft.Toolkit.Uwp.UI.Controls
{
/// <summary>
/// Display <see cref="MetadataUnit"/>s separated by bullets.
/// Display <see cref="MetadataItem"/>s separated by bullets.
/// </summary>
[TemplatePart(Name = TextContainerPart, Type = typeof(TextBlock))]
public sealed class MetadataControl : Control
Expand All @@ -38,13 +38,22 @@ public sealed class MetadataControl : Control
new PropertyMetadata(", ", OnPropertyChanged));

/// <summary>
/// The DP to store the <see cref="MetadataUnits"/> property value.
/// The DP to store the <see cref="Items"/> property value.
/// </summary>
public static readonly DependencyProperty MetadataUnitsProperty = DependencyProperty.Register(
nameof(MetadataUnits),
typeof(IEnumerable<MetadataUnit>),
public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register(
nameof(Items),
typeof(IEnumerable<MetadataItem>),
typeof(MetadataControl),
new PropertyMetadata(null, OnMetadataUnitsChanged));
new PropertyMetadata(null, OnMetadataItemsChanged));

/// <summary>
/// The DP to store the TextBlockStyle value.
/// </summary>
public static readonly DependencyProperty TextBlockStyleProperty = DependencyProperty.Register(
nameof(TextBlockStyle),
typeof(Style),
typeof(MetadataControl),
new PropertyMetadata(null));

private const string TextContainerPart = "TextContainer";

Expand All @@ -60,7 +69,7 @@ public MetadataControl()
}

/// <summary>
/// Gets or sets the separator to display between the <see cref="MetadataUnit"/>.
/// Gets or sets the separator to display between the <see cref="MetadataItem"/>.
/// </summary>
public string Separator
{
Expand All @@ -78,13 +87,22 @@ public string AccessibleSeparator
}

/// <summary>
/// Gets or sets he <see cref="MetadataUnit"/> to display in the control.
/// Gets or sets the <see cref="MetadataItem"/> to display in the control.
/// If it implements <see cref="INotifyCollectionChanged"/>, the control will automatically update itself.
/// </summary>
public IEnumerable<MetadataUnit> MetadataUnits
public IEnumerable<MetadataItem> Items
vgromfeld marked this conversation as resolved.
Show resolved Hide resolved
{
get => (IEnumerable<MetadataItem>)GetValue(ItemsProperty);
set => SetValue(ItemsProperty, value);
}

/// <summary>
/// Gets or sets the <see cref="Style"/> to use on the inner <see cref="TextBlock"/> control.
/// </summary>
public Style TextBlockStyle
{
get => (IEnumerable<MetadataUnit>)GetValue(MetadataUnitsProperty);
set => SetValue(MetadataUnitsProperty, value);
get => (Style)GetValue(TextBlockStyleProperty);
set => SetValue(TextBlockStyleProperty, value);
}

/// <inheritdoc/>
Expand All @@ -94,7 +112,7 @@ protected override void OnApplyTemplate()
Update();
}

private static void OnMetadataUnitsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
private static void OnMetadataItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = (MetadataControl)d;
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args) => control.Update();
Expand Down Expand Up @@ -127,7 +145,7 @@ private void Update()

_textContainer.Inlines.Clear();

if (MetadataUnits is null)
if (Items is null)
{
AutomationProperties.SetName(_textContainer, string.Empty);
NotifyLiveRegionChanged();
Expand All @@ -136,7 +154,7 @@ private void Update()

Inline unitToAppend;
var accessibleString = new StringBuilder();
foreach (var unit in MetadataUnits)
foreach (var unit in Items)
{
if (_textContainer.Inlines.Count > 0)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls">
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls">

<Style TargetType="controls:MetadataControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:MetadataControl">
<TextBlock x:Name="TextContainer" TextWrapping="Wrap" />
<TextBlock x:Name="TextContainer"
Style="{TemplateBinding TextBlockStyle}" />
</ControlTemplate>
</Setter.Value>
</Setter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
namespace Microsoft.Toolkit.Uwp.UI.Controls
{
/// <summary>
/// A unit of metadata to display in <see cref="MetadataControl"/>.
/// An item to display in <see cref="MetadataControl"/>.
/// </summary>
public struct MetadataUnit
public struct MetadataItem
{
/// <summary>
/// Gets or sets the label of the unit.
/// Gets or sets the label of the item.
/// </summary>
public string Label { get; set; }

/// <summary>
/// Gets or sets the automation name that will be set on the unit.
/// Gets or sets the automation name that will be set on the item.
/// If not set, <see cref="Label"/> will be used.
/// </summary>
public string AccessibleLabel { get; set; }

/// <summary>
/// Gets or sets the command associated to the unit.
/// If null, the unit will be displayed as a text field.
/// If set, the unit will be displayed as an hyperlink.
/// Gets or sets the command associated to the item.
/// If null, the item will be displayed as a text field.
/// If set, the item will be displayed as an hyperlink.
/// </summary>
public ICommand Command { get; set; }

Expand Down