我有用于处理person对象的Wpf-App。 Person Struct是Sql-server表,我使用Linq-to-Sql作为我的项目(所以dbml中有类引用person)。
我有更新或插入人员的表格(简单的模态窗口)。在这个窗口中,我有Peoperty
,其当前值为人。
public Person CurrentPerson { get; set; }
所以我要找的是:
如何根据
CurrentPerson.FullName
绑定此窗口的标题?如果CurrentPerson.FullName已更改,绝对必须更改Window Title!
修改:更多信息
我想更改CurrentPerson.Name
上的窗口标题基础,而不是与CurrentPerson.Name
相同。所以这可能会改变一些东西。此外,我在查找this和Question之前搜索了有关更改标题的部分内容。但我需要根据价值改变Title的某些部分。
答案 0 :(得分:2)
编辑:删除了旧答案,因为我完全误解了这个问题。
问题很可能出在你的约束力上。我认为绑定失败是因为它无法决定在哪里搜索CurrentUser(binding source)
。你能试试吗 -
编辑2:您可以为控件命名,然后在Binding Element中使用该名称,例如:
<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
[这是VS2010;应该与其他人相似。]
并检查是否存在绑定错误,如果存在绑定错误。
答案 1 :(得分:2)
首先,您的codebehind或viewmodel应该实现INotifyPropertyChanged
。之后,实现这样的属性WindowTitle
:
public string WindowTitle
{
get { return "Some custom prefix" + CurrentPerson.FullName; }
}
在此之后,每当您更改FullName
上的CurrentPerson
时,只需举办PropertyChanged
活动,例如:
Person _currentPerson;
public Person CurrentPerson
{
get { return _currentPerson; }
set
{
_currentPerson = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("WindowTitle"));
}
}
编辑:请发布你的xaml代码进行绑定,看看你在新手的帖子上的评论,似乎是罪魁祸首。另外,请检查您是否将Window
&#39; s DataContext
设置为自身。
答案 2 :(得分:1)
你可以这样做:
Person _currentPerson;
public Person CurrentPerson
{
get { return _currentPerson; }
set
{
_currentPerson = value;
this.Title = value.FullName;
}
}