开发者

How to combine binding from custom data context with custom markup

开发者 https://www.devze.com 2023-01-29 00:16 出处:网络
I want to combine binding from my custom data context which contains ViewModel class and ResourceProvider class. Custom data context is set as window DataContext.

I want to combine binding from my custom data context which contains ViewModel class and ResourceProvider class. Custom data context is set as window DataContext.

I use it that way:

<Button x:Name="btnShow" Content="Show" Command="{Binding View.HandleShow}"/>

Which View is property from dataContext. I want to use localization by custom data context using minimum markup and set ResourceProvider from other source in code that I created my own data context

Is there any possibility to do it in something which is similar to that line of code:

 <TextBlock Text="{Binding Res.Key=test}" />

My resource provider inherits from markup extension with one Property: Key.

Than开发者_如何学Goks for any advice


You can create a custom markup extension using the following code :

public class LocalizedBinding : MarkupExtension
{
    public String Key { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        IProvideValueTarget target = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));

        //use target.TargetObject and target.TargetProperty to provide value based on Key
    }
}

and use it like :

<TextBlock Text="{local:LocalizedBinding Key=SomeKey}" />


I try that solution but i prefer avoid prefix local because localizedBinding came from different source and use IoC pattern because of that I create CustomDataContext

0

精彩评论

暂无评论...
验证码 换一张
取 消