开发者

Can I bind silverlight controls to DynamicObject.Properties?

I have a DynamicObject class that is binded to some controls in silverlight xaml.

class Localizer
{
    public S开发者_如何学JAVAtrings Strings { get; set; }
}

public class Strings : DynamicObject
{
    Dictionary<string, string> values;

    public Strings(Dictionary<string, string> values)
    {
        this.values = values;
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        string value;
        bool success = values.TryGetValue(binder.Name, out value);
        result = value;
        return success;
    }
}

and bind some controls to this like this:

<TextBlock Text="{Binding Strings.User, Source={StaticResource Localizer}}"/>

When I run the application I get this error in VS output:

System.Windows.Data Error: BindingExpression path error: 'Login' property not found on '....Localization.StringsResource' '....Localization.StringsResource' (HashCode=10857028). BindingExpression: Path='Strings.Login' DataItem='....Localization.Localizer' (HashCode=30604389); target element is 'System.Windows.Controls.Button' (Name='btnLogin'); target property is 'Content' (type 'System.Object').

but when read property in code like this:

string UserName = localizer.Strings.Login;

It work correctly, is it possible to bind controls to DynamicObject in xaml?


Unfortunately this is a bug in silverlight, :(
Databinding to Dynamic Objects is Broken


In Silverlight 4, you can't data bind properties, but you can data bind indexers. Since I assume you aren't supporting live data changes in that your Strings object, as it doesn't support INotifiyPropertyChanged you could just return the dictionary and use the indexer binding syntax.

But if you did need to support INotifiyPropertyChanged, silverlight has syntax for notifying changes of specific indexer values new PropertyChangedEventArgs("Item["+key+"]") that you could use in the Strings object.

Silverlight 5 may have a easy workaround, or a difficult one, it's hard to say at this point.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜