简体   繁体   中英

Subclassed Binding works at runtime, but not in designer

In order to save typing and make code more readable, I subclassed the Binding class to make it use a specific source:

public class MyBinding : Binding
{
    private void SetSource()
    {
        Source = Application.Current.Resources["Translator"];
    }

    public TBinding()
    {
        SetSource();
    }

    public TBinding(string path) : base(path)
    {
        SetSource();
    }
}

So instead of writing this:

<TextBlock Text="{Binding Path='hello', Source={StaticResource Translator}}"/>

I can write this:

<TextBlock Text="{local:MyBinding Path='hello'}"/>

That works perfectly at runtime, but the designer doesn't show the correct text. Instead it shows the type of the MyBinding class.

Is there any way to make this work in the Visual Studio designer?

Yes, you can use d:DataContext or d:DesignData in your XAML to pull in sample data. There is an article describing this here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM