简体   繁体   中英

WPF: Binding TextBox Text to a sub-element of a Property with WCF?

I have a TextBox which I'm trying to bind to a element of a table property 'regimeAlias' is a column with the tbRegimes table which I have mapped with Entity Framework:

<TextBox Text="{Binding NewRegime.regimeAlias, Mode=TwoWay}"/>

Exposed property in my ViewModel:

private tbRegime _NewRegime;
public tbRegime NewRegime
{
    get { return _NewRegime; }
    set
    {
        _NewRegime = value;
        OnPropertyChanged("NewRegime");
    }
}  

Lastly, here's the WCF Service Reference auto-generated code class:

public partial class tbRegime : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {

//blah blah blah

[System.Runtime.Serialization.DataMemberAttribute()]
public string regimeAlias {
    get {
        return this.regimeAliasField;
    }
    set {
        if ((object.ReferenceEquals(this.regimeAliasField, value) != true)) {
            this.regimeAliasField = value;
            this.RaisePropertyChanged("regimeAlias");
        }
    }
}

The setter never gets hit. Is this because each element within the NewRegime object needs to raise PropertyChanged and if so is there an easy workaround without adding a further DTO layer to my code?

Edit3: with the post from your regimeAlias code. i have to say your binding should work. but of course if you wanna debug you have to set the breakpoint in your regimeAlias setter


<TextBox Text="{Binding NewRegime.regimeAlias, Mode=TwoWay}"/>

this code means, you bind to a Public Property regimeAlias in your class tbRegime . your setter for NewRegime will never hit because you dont bind to it.

so check your tbRegime class property setter for regimeAlias.

EDIT: the DataContext of the TextBox is of course an object with the Public Property NewRegime, but like i said if you use dot notation in your binding the last property is the one you bind to :)

EDIT: you dont have much ways to workaround:) if you let the binding like you did, you need a model with a public property regimeAlias and it should implement INotifyPropertyChanged.

if you wanna wrap the regimeAlias Property then you have the problem the you have to raise OnPropertyChanged("MyRegimeAlias") at the right point.

public string MyRegimeAlias
{
    get { return _NewRegime.regimeAlias; }
    set
    {
        _NewRegime.regimeAlias = value;
        OnPropertyChanged("MyRegimeAlias");
    }
}

xaml

<TextBox Text="{Binding MyRegimeAlias, Mode=TwoWay}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM