开发者

C# WPF Switch Resource Programmatically

i`m trying to design a dialog window which will display different messages depending on what cas开发者_开发技巧e will be true.

so for example when i have a

<CustomDialog Content="{Binding Path=Name}"/>

is there a possibility to change the Binding Path=Name to Path=Whatever or how do you implement something like that ? When the Control should use other resources on runtime.

--------------edit

I ok i`ll try to describe my problem better ;) I have an ResourceDictionary with strings for example

<System:String x:Key="Message1">Message1</System:String>
<System:String x:Key="Message2">Message2</System:String>
<System:String x:Key="Message3">Message3</System:String>

So when I now call my UserControl Doing it customdialog.visibility = true; for example

<CustomDialog Text=”” />

I want to define which key from the resourcedictionary is taken when the dialog popups up.

something like customdialog.text = Message1; but Loaded from the ResourceDictionary

is that possible or is there an better way of doing something like this ?


You may provide another content to the same property Name at runtime in code-behind. Suppose you have Initialize (or may be Show) method in your CustomDialog and the last one implements INotifyPropertyChanged:

public class CustomDialog : INotifyPropertyChanged
{
    //Your implementation of class goes here

    public void Initialize(string message)
    {
        Name = message;
        Visibility = Visibility.Visible;
    }

    public string Name
    {
        get {return _name;}
        set
        {
            if (_name != value)
            {
                _name = value;
                raiseOnPropertyChanged("Name");
            }
        }
    }

    //Your implementation of class goes here
}

In method Initialize there will be updated Name property and your control will be shown. When there will be setting of Name property must be raise PropertyChanged event which will tell presentation that binded value has updated and to reflect it in the UI.


The easiest way I can think of would be to bind to the parent item, not to a child property, and then use a DataTemplateSelector to select a different template at run-time, depending on some condition involving the bound object or its properties.

Alternatively, if the Content has well defined types, you only need to define DataTemplates with specific data types, and they will be automatically used to display objects of those types.

Not knowing more about the context I can't be much more specific, but if you search for more information on DataTemplates and DataTemplateSelectors you should be fine - you can find a lot of useful information here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜