2

I have Wpf-App for working on person object. Person Struct is Sql-server table and I use Linq-to-Sql for my project(so there is class in dbml refer to person).

I have form for update or inserting persons(simple modal window). In this window I have Peoperty which have current value of person.

public Person CurrentPerson { get; set; }

So what I look for is :

How bind title of this window base on CurrentPerson.FullName? and absolutely Window Title must change if CurrentPerson.FullName Changed!

Edit: More Info
I want to change window title base on CurrentPerson.Name not set equally same as CurrentPerson.Name. So this might be change something. Also I searched before find this and this Question about changing part of Title. but I need to change some part of Title base on value.

4

3 回答 3

2

Edit: Deleted the old answer because I completely misunderstood the question.

The problem is likely to be in your binding. I think the binding fails because it cant decide where to search for CurrentUser(binding source). Can you try this -

Edit 2: You can name your control and then use that name in Binding Element, like:

<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">

If this doesn't work can you enable WPF debugging for binding expressions by going to:

Tools -> Options -> Debugging -> Output Window -> WPF Trace Settings [this is for VS2010; should be similar for others.]

and check if there is a binding error and if so what it is.

于 2012-04-09T06:33:25.427 回答
2

Firstly, your codebehind or viewmodel should implement INotifyPropertyChanged. After that, implement a property WindowTitle like this:

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

After this, whenever your change the FullName on your CurrentPerson, just throw a PropertyChanged event, for example:

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

EDIT: Please post your xaml code for the binding, which looking at your comment on the post of Novice, seems like the culprit. Also, check that you are setting the Window's DataContext to itself.

于 2012-04-10T08:22:10.790 回答
1

You can do it like this:

Person _currentPerson;
public Person CurrentPerson 
{
    get { return _currentPerson; }
    set
    {
        _currentPerson = value;
        this.Title = value.FullName;
    }
}
于 2012-04-09T06:10:26.250 回答