2012-04-09 41 views
1

我有用于处理人物的Wpf-App。 Person Struct是Sql-server表,我使用Linq-to-Sql作为我的项目(因此dbml中的类指的是人)。基于信息类的WPF窗口标题绑定?

我有表单更新或插入人(简单的模态窗口)。在这个窗口中,我有Peoperty其中有当前人的价值。

public Person CurrentPerson { get; set; } 

所以我寻找的是:

如何绑定在CurrentPerson.FullName这个窗口基地称号?并且如果CurrentPerson.FullName已更改,绝对Window标题必须更改!

编辑:更多信息
我想在CurrentPerson.Name改变窗口标题基地未设置为CurrentPerson.Name同样相同。所以这可能会改变一些事情。另外我搜索之前找到this和这个Question关于改变标题的一部分。但我需要改变标题基于价值的一部分。

回答

1

首先,你的codebehind或viewmodel应该实现INotifyPropertyChanged。在此之后,执行财产WindowTitle这样的:

public string WindowTitle 
{ 
    get { return "Some custom prefix" + CurrentPerson.FullName; } 
} 

在此之后,每当你的变化FullNameCurrentPerson,只是抛出一个PropertyChanged事件,例如:

Person _currentPerson; 
public Person CurrentPerson 
{ 
    get { return _currentPerson; } 
    set 
    { 
     _currentPerson = value; 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs("WindowTitle")); 
    } 
} 

编辑:请发表您的绑定的xaml代码,看着你对新手帖子的评论,似乎是罪魁祸首。另外,请检查您是否将WindowDataContext设置为自己。

+0

谢谢我没有为当前窗口设置'DataContext'。设置后它运行良好。 – Rev 2012-04-10 11:09:22

1

你可以这样说:

Person _currentPerson; 
public Person CurrentPerson 
{ 
    get { return _currentPerson; } 
    set 
    { 
     _currentPerson = value; 
     this.Title = value.FullName; 
    } 
} 
+0

谢谢你好。但我想绑定! – Rev 2012-04-09 06:27:13

+1

@Rev我想看看有人能做到这一点,或解释为什么这是不可能的。看来正常的绑定方法在这种情况下不起作用,但我不知道为什么。 – McGarnagle 2012-04-09 06:50:15

+0

是的,我尝试甚至依赖道具或转换器,但我不能这样做。我不知道该怎么做,也许有人可以帮助我! – Rev 2012-04-09 07:23:06

2

编辑:删除旧的答案,因为我完全误解了这个问题。

问题可能在于你的约束力。我认为绑定失败,因为它不能决定在哪里搜索CurrentUser(binding source)。你可以试试这个 -

编辑2:可以命名你的控制,然后使用该名称在绑定元素,如:

<Window x:Class="TestApp.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="{Binding ElementName=MW,Path=CurrentUser.FullName, StringFormat='Welcome \{0\}!'}"   
    Name="MW"> 

如果这不起作用,你可以启用调试WPF绑定表达式转到:

Tools -> Options -> Debugging -> Output Window -> WPF Trace Settings [this for for VS2010;应该与其他人类似。]

并检查是否存在绑定错误,如果是这样。

+0

@Rev:哦,我错过了Linq-to-Sql位。这将解释反对票(这很好)。我希望我现在能理解这个问题。 – NoviceProgrammer 2012-04-09 13:30:01

+0

@Rev:不知道您是否使用标题栏或常规标准窗口的自定义控件。 – NoviceProgrammer 2012-04-10 08:18:23

+0

+1为wpf调试,也是你的注意力 – Rev 2012-04-10 10:18:14