This commit is contained in:
Dan Clark
2024-11-22 18:52:25 +00:00
parent 8a2497be82
commit e809c22cb7
15 changed files with 145 additions and 31 deletions
+16 -5
View File
@@ -23,21 +23,33 @@ public static class ContentTemplateBinding
{
if (dependencyObject is FrameworkElement content)
{
IActivation? cachedActivation = null;
void HandleLoaded(object sender, RoutedEventArgs args)
{
if (content.DataContext is IActivation activation)
if (sender is FrameworkElement content)
{
content.Loaded -= HandleLoaded;
activation.IsActive = true;
if (content.DataContext is IActivation activation)
{
cachedActivation = activation;
activation.IsActive = true;
}
}
}
void HandleUnloaded(object sender, RoutedEventArgs args)
{
if (content.DataContext is IActivation activation)
if (cachedActivation is not null)
{
cachedActivation.IsActive = false;
}
if (sender is FrameworkElement content)
{
cachedActivation = null;
content.Unloaded -= HandleUnloaded;
activation.IsActive = false;
}
}
@@ -45,7 +57,6 @@ public static class ContentTemplateBinding
{
content.Loaded += HandleLoaded;
content.Unloaded += HandleUnloaded;
}
else
{
+36
View File
@@ -0,0 +1,36 @@
using System;
using System.Globalization;
namespace Toolkit.UI.WinUI;
public class StringFormatConverter :
ValueConverter<object, object>
{
public string? StringFormat { get; set; }
protected override object? ConvertTo(object value,
Type? targetType,
object? parameter,
string? language)
{
if (value is null)
{
return null!;
}
if (string.IsNullOrEmpty(StringFormat))
{
return value.ToString()!;
}
try
{
CultureInfo culture = string.IsNullOrWhiteSpace(language) ? CultureInfo.InvariantCulture : new CultureInfo(language);
return string.Format(culture, StringFormat, value);
}
catch
{
return value;
}
}
}
+40
View File
@@ -0,0 +1,40 @@
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Markup;
using System;
namespace Toolkit.UI.WinUI;
public abstract class ValueConverter<TSource, TTarget> :
MarkupExtension,
IValueConverter
{
public object? Convert(object value,
Type targetType,
object parameter,
string language) =>
ConvertTo((TSource)value, targetType, parameter, language);
public object? ConvertBack(object value,
Type targetType,
object parameter,
string language) =>
ConvertBackTo((TTarget)value, targetType, parameter, language);
public TTarget? Convert(TSource value) =>
ConvertTo(value, null, null, null);
public TSource? ConvertBack(TTarget value) =>
ConvertBackTo(value, null, null, null);
protected virtual TTarget? ConvertTo(TSource value,
Type? targetType,
object? parameter,
string? language) => default;
protected virtual TSource? ConvertBackTo(TTarget value,
Type? targetType,
object? parameter,
string? language) => default;
protected override object ProvideValue() => this;
}