0

The markup extension that brought me to ask this is Catel's LanguageBinding.

I was until now using Infralution's localization assembly, which works more or less the same way.

Catel:

<TextBlock Text="{LanguageBinding MyText}"/>

Infralution:

<TextBlock Text="{Resx MyText}"/>

But as you can see, the markup extension is way shorter to write, thus less prone to typos.

So I wanted to know if there was any way to be able to use LanguageBinding with another markup extension word, like:

Ideal:

<TextBlock Text="{LB MyText}"/>

I'm well aware of readability issues and such, it's an example.

3
  • 1
    How about creating a class with the desired name that inherits from the original markup extension class? Oct 2, 2015 at 9:01
  • Why don't you just use Intellisense?
    – wingerse
    Oct 2, 2015 at 9:02
  • @Pieter That was my first guess, but I wanted to see if I could get more input. Empereur I'm working on a pretty slow VM, and sometimes Intellisense takes a few seconds to pop, or even doesn't show up, that plus the fact that if I need to prepend a namespace, I've got to use Intellisense twice.
    – Kilazur
    Oct 2, 2015 at 10:18

2 Answers 2

2

That's not possible to do directly in XAML but you can derive a class from LanguageBinding and use it. Here's an example for shortening the StaticResource Markup Extension.

class SR : StaticResourceExtension
{
    public SR() {}

    public SR(object resourceKey)
        :base(resourceKey)
    { }
}

Now you need can use something like {local:SR} as an "alias".

3
  • 1
    You can also add an XmlnsDefinition attribute to your assembly to make certain namespaces available in the default XML namespace of your XAML files, for example: [assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "MyNamespace")] - so you don't even need to use a local: prefix. Oct 2, 2015 at 10:48
  • @Pieter this is exactly what I'm working on right now, but I can't seem to make my XAML files compile with these definitions, even when they are in another referenced assembly.
    – Kilazur
    Oct 2, 2015 at 12:56
  • Following question on that matter, if someone is interested in answering!
    – Kilazur
    Oct 2, 2015 at 13:22
0

You can inherit MarkupExtension to create your own custom Binding tag

[MarkupExtensionReturnType(typeof(object))]
public class LBBinding : MarkupExtension
{
    private Binding _binding = new Binding();

    public Binding Binding
    {
        get { return _binding; }
        set { _binding = value; }
    }

    public PropertyPath Path
    {
        get { return _binding.Path; }
        set { _binding.Path = value; }
    }



<TextBox Text="{customBinding:LBBinding Path=DummyString}"></TextBox>

You should also override ProvideValue method from MarkupExtension. This method will be trigger whenever WPF perform the actual binding. Use the IServiceProvider to get back your DependencyObject (Control) and the DependencyProperty (Your binding property). Then you can do all the magic you want with those 2 information.

    public override object ProvideValue(IServiceProvider provider)
    {
       var service = (IProvideValueTarget)provider.GetService(typeof(IProvideValueTarget));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.