How to escape characters in Xaml MarkupExtension?

William Liu 266 Reputation points
2021-09-30T03:41:25.767+00:00

I`m interesting on how to escape the special characters, e.g. &, a single left/right Parenthesis, backslash , and so on.

Here is my test code snippet. The purpose is using the converter to output the string you input on the xaml MarkupExtension code.

//ReturnInputConverter.cs

public class ReturnInputConverter : MarkupExtension, IValueConverter
{
    public string Input { get; set; } = string.Empty;

    public override object ProvideValue(IServiceProvider serviceProvider) => this;

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => Input;

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;
}

// MainWindow.xaml

<StackPanel>
    <CheckBox x:Name="CheckBox" />
    <TextBlock Text="{Binding ElementName=CheckBox, Path=IsChecked, Converter={escapeInXaml:ReturnInputConverter Input=input-here-for-preview}}" />
</StackPanel>

repo EscapeInXaml

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,681 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
767 questions
{count} votes