Skip to content

Commit 32f36c8

Browse files
committed
Replaced ToolbarFormatterActiveConverter to a generic TypeToObjectConverter.
1 parent c80e154 commit 32f36c8

File tree

6 files changed

+171
-52
lines changed

6 files changed

+171
-52
lines changed

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/TextToolbar/TextToolbar.bind

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@
1212
mc:Ignorable="d">
1313

1414
<Page.Resources>
15-
<converters:ToolbarFormatterActiveConverter x:Key="IsMarkdownFormatter"
16-
FormatterType="Microsoft.Toolkit.Uwp.UI.Controls.TextToolbarFormats.MarkDown.MarkDownFormatter" />
15+
<converters:TypeToObjectConverter x:Key="IsMarkdownFormatter"
16+
Type="markDown:MarkDownFormatter">
17+
<converters:TypeToObjectConverter.TrueValue>
18+
<Visibility>Visible</Visibility>
19+
</converters:TypeToObjectConverter.TrueValue>
20+
<converters:TypeToObjectConverter.FalseValue>
21+
<Visibility>Collapsed</Visibility>
22+
</converters:TypeToObjectConverter.FalseValue>
23+
</converters:TypeToObjectConverter>
1724
</Page.Resources>
1825

1926
<Grid x:Name="MainGrid"

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/TextToolbar/TextToolbarPage.xaml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@
1111
mc:Ignorable="d">
1212

1313
<Page.Resources>
14-
<converters:ToolbarFormatterActiveConverter x:Key="IsMarkdownFormatter"
15-
FormatterType="Microsoft.Toolkit.Uwp.UI.Controls.TextToolbarFormats.MarkDown.MarkDownFormatter" />
14+
<converters:TypeToObjectConverter x:Key="IsMarkdownFormatter"
15+
Type="markDown:MarkDownFormatter">
16+
<converters:TypeToObjectConverter.TrueValue>
17+
<Visibility>Visible</Visibility>
18+
</converters:TypeToObjectConverter.TrueValue>
19+
<converters:TypeToObjectConverter.FalseValue>
20+
<Visibility>Collapsed</Visibility>
21+
</converters:TypeToObjectConverter.FalseValue>
22+
</converters:TypeToObjectConverter>
1623
</Page.Resources>
1724

1825
<Grid>

Microsoft.Toolkit.Uwp.UI.Controls/TextToolbar/ToolbarFormatterActiveConverter.cs

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using Windows.UI.Xaml;
7+
using Windows.UI.Xaml.Data;
8+
9+
namespace Microsoft.Toolkit.Uwp.UI.Converters
10+
{
11+
/// <summary>
12+
/// This class returns an object or another, depending on whether the type of the provided value matches another provided Type.
13+
/// </summary>
14+
public class TypeToObjectConverter : DependencyObject, IValueConverter
15+
{
16+
/// <summary>
17+
/// Identifies the <see cref="TrueValue"/> property.
18+
/// </summary>
19+
public static readonly DependencyProperty TrueValueProperty =
20+
DependencyProperty.Register(nameof(TrueValue), typeof(object), typeof(TypeToObjectConverter), new PropertyMetadata(null));
21+
22+
/// <summary>
23+
/// Identifies the <see cref="FalseValue"/> property.
24+
/// </summary>
25+
public static readonly DependencyProperty FalseValueProperty =
26+
DependencyProperty.Register(nameof(FalseValue), typeof(object), typeof(TypeToObjectConverter), new PropertyMetadata(null));
27+
28+
/// <summary>
29+
/// Identifies the <see cref="Type"/> property.
30+
/// </summary>
31+
public static readonly DependencyProperty TypeProperty =
32+
DependencyProperty.Register(nameof(Type), typeof(Type), typeof(TypeToObjectConverter), new PropertyMetadata(typeof(object)));
33+
34+
/// <summary>
35+
/// Gets or sets the value to be returned when the type of the provided value matches <see cref="Type"/>.
36+
/// </summary>
37+
public object TrueValue
38+
{
39+
get { return GetValue(TrueValueProperty); }
40+
set { SetValue(TrueValueProperty, value); }
41+
}
42+
43+
/// <summary>
44+
/// Gets or sets the value to be returned when the type of the provided value does not match <see cref="Type"/>.
45+
/// </summary>
46+
public object FalseValue
47+
{
48+
get { return GetValue(FalseValueProperty); }
49+
set { SetValue(FalseValueProperty, value); }
50+
}
51+
52+
/// <summary>
53+
/// Gets or sets the Type used to compare the type of the provided value.
54+
/// </summary>
55+
public Type Type
56+
{
57+
get { return (Type)GetValue(TypeProperty); }
58+
set { SetValue(TypeProperty, value); }
59+
}
60+
61+
/// <summary>
62+
/// Convert the <paramref name="value"/>'s Type to an other object.
63+
/// </summary>
64+
/// <param name="value">The source data being passed to the target.</param>
65+
/// <param name="targetType">The type of the target property, as a type reference.</param>
66+
/// <param name="parameter">An optional parameter to be used to invert the converter logic.</param>
67+
/// <param name="language">The language of the conversion.</param>
68+
/// <returns>The value to be passed to the target dependency property.</returns>
69+
public object Convert(object value, Type targetType, object parameter, string language)
70+
{
71+
var typeMatches = value != null && Type.Equals(value.GetType());
72+
73+
// Negate if needed
74+
if (ConverterTools.TryParseBool(parameter))
75+
{
76+
typeMatches = !typeMatches;
77+
}
78+
79+
return ConverterTools.Convert(typeMatches ? TrueValue : FalseValue, targetType);
80+
}
81+
82+
/// <summary>
83+
/// Not implemented.
84+
/// </summary>
85+
/// <param name="value">The source data being passed to the target.</param>
86+
/// <param name="targetType">The type of the target property, as a type reference.</param>
87+
/// <param name="parameter">Optional parameter. Not used.</param>
88+
/// <param name="language">The language of the conversion. Not used.</param>
89+
/// <returns>The value to be passed to the target dependency property.</returns>
90+
public object ConvertBack(object value, Type targetType, object parameter, string language)
91+
{
92+
throw new NotImplementedException();
93+
}
94+
}
95+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.Toolkit.Uwp.UI.Converters;
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
8+
using Windows.UI.Xaml;
9+
10+
namespace UnitTests.Converters
11+
{
12+
[TestClass]
13+
public class Test_TypeToObjectConverter
14+
{
15+
[TestCategory("Converters")]
16+
[UITestMethod]
17+
public void Test_StringConvertStringToVisibility()
18+
{
19+
var converter = new TypeToObjectConverter
20+
{
21+
TrueValue = Visibility.Visible,
22+
FalseValue = Visibility.Collapsed,
23+
Type = typeof(string)
24+
};
25+
var result = converter.Convert("anything", typeof(Visibility), null, "en-us");
26+
Assert.AreEqual(Visibility.Visible, result);
27+
}
28+
29+
[TestCategory("Converters")]
30+
[UITestMethod]
31+
public void Test_StringDoesntConvertBoolToVisibility()
32+
{
33+
var converter = new TypeToObjectConverter
34+
{
35+
TrueValue = Visibility.Visible,
36+
FalseValue = Visibility.Collapsed,
37+
Type = typeof(string)
38+
};
39+
var result = converter.Convert(true, typeof(Visibility), null, "en-us");
40+
Assert.AreEqual(Visibility.Collapsed, result);
41+
}
42+
43+
[TestCategory("Converters")]
44+
[UITestMethod]
45+
public void Test_StringConvertStringToVisibilityWithNegateTrue()
46+
{
47+
var converter = new TypeToObjectConverter
48+
{
49+
TrueValue = Visibility.Visible,
50+
FalseValue = Visibility.Collapsed,
51+
Type = typeof(string)
52+
};
53+
var result = converter.Convert("anything", typeof(Visibility), "true", "en-us");
54+
Assert.AreEqual(Visibility.Collapsed, result);
55+
}
56+
}
57+
}

UnitTests/UnitTests.UWP/UnitTests.UWP.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
<Compile Include="Converters\Test_EmptyCollectionToObjectConverter.cs" />
134134
<Compile Include="Converters\Test_EmptyStringToObjectConverter.cs" />
135135
<Compile Include="Converters\Test_StringFormatConverter.cs" />
136+
<Compile Include="Converters\Test_TypeToObjectConverter.cs" />
136137
<Compile Include="Extensions\Helpers\ObjectWithNullableBoolProperty.cs" />
137138
<Compile Include="Extensions\Test_BitmapIconExtensionMarkupExtension.cs" />
138139
<Compile Include="Extensions\Test_FontIconSourceExtensionMarkupExtension.cs" />

0 commit comments

Comments
 (0)