2010-09-03 69 views
0

我试图设计一个对话框窗口,它将显示不同的消息,这取决于什么情况是真实的。编程的C#WPF开关资源

因此,例如,当我有一个

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

有更改绑定路径=名称路径=无论或者你如何实现这样的事情的可能性?当控件应该在运行时使用其他资源。

--------------编辑

我确定我心底尝试更好地描述我的问题;) 我有例如串的资源字典

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

因此,当我现在打电话给我的UserControl 做到这一点customdialog.visibility = true;例如

<CustomDialog Text=”” /> 

我想定义在弹出对话框时从resourcedictionary中获取哪个密钥。

类似于customdialog.text = Message1;但从ResourceDictionary加载

是可能或有没有更好的方式来做这样的事情?

回答

2

您可以在代码隐藏时在运行时向同一个属性Name提供另一个内容。假设你有Initialize(或者可能是Show)方法在你CustomDialog,最后一个实现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 
} 

在方法初始化会有更新Name财产和你的控制将被显示。当设置为Name时,必须提高属性PropertyChanged事件,该事件将告诉演示文稿绑定值已更新并在UI中反映它。

1

我能想到的最简单的方法是绑定到父项而不是子属性,然后使用DataTemplateSelector在运行时选择不同的模板,具体取决于涉及绑定对象的一些条件或其属性。或者,如果内容具有良好定义的类型,则只需要使用特定数据类型定义DataTemplates,并且它们将自动用于显示这些类型的对象。

不知道更多关于上下文我不能更具体,但如果你搜索DataTemplates和DataTemplateSelectors的更多信息,你应该没问题 - 你可以找到很多有用的信息here