![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
| |
| | #1 (permalink) |
| | TwoWay DataBinding to property having private set Is it possible to make TwoWay DataBinding to property having private set Below is the code of BindingObject class that has property Name whose Get is public but Set is private but there is one Public method in this class SetProperty which can be called to set value of Name property. So my requirement is to make TwoWay DataBinding with this property. Is there any workaround that binding can call SetProperty rather calling Set of Name property //Business Object Code public class BindingObject : INotifyPropertyChanged { string name; public BindingObject() { this.name = "WPF"; } public string Name { get { return this.name; } private set { this.name = value; if (PropertyChanged!=null) { PropertyChanged(this, new PropertyChangedEventArgs("Name")); } } } public void SetProperty(string value) { this.name = value; } } //GUI Code InitializeGUI() { TextBox textBox = new TextBox(); Binding binding = new Binding("Name"); binding.Source = new BindingObject(); binding.Mode = BindingMode.TwoWay; textBox.SetBinding(TextBox.TextProperty, binding);//throws exception as Name property has private set } Thanks Gaurav |
My System Specs![]() |
| | #2 (permalink) |
| | Re: TwoWay DataBinding to property having private set Since you have a private 'set', every attempt to set the variable will fail, wetherver your are using databinding or not. I think the only way you can databind an object with private set accessor is using oneway databinding (from the BindingObject to the TextBox.Text) since there is no way for the TextBox to set the Name property. On Feb 26, 11:48 pm, gauravgoyal.a...@xxxxxx wrote: Quote: > Is it possible to make TwoWay DataBinding to property having private > set > > Below is the code of BindingObject class that has property Name whose > Get is public but Set is private but there is one Public method in > this class SetProperty which can be called to set value of Name > property. > So my requirement is to make TwoWay DataBinding with this property. Is > there any workaround that binding can call SetProperty rather calling > Set of Name property > > //Business Object Code > public class BindingObject : INotifyPropertyChanged > { > string name; > > public BindingObject() > { > this.name = "WPF"; > } > > public string Name > { > get > { > return this.name; > } > private set > { > this.name = value; > if (PropertyChanged!=null) > { > PropertyChanged(this, new > PropertyChangedEventArgs("Name")); > } > } > } > > public void SetProperty(string value) > { > this.name = value; > } > > } > > //GUI Code > InitializeGUI() > { > TextBox textBox = new TextBox(); > Binding binding = new Binding("Name"); > binding.Source = new BindingObject(); > binding.Mode = BindingMode.TwoWay; > textBox.SetBinding(TextBox.TextProperty, binding);//throws > exception as Name property has private set > > } > > Thanks > Gaurav |
My System Specs![]() |
| | #3 (permalink) |
| | Re: TwoWay DataBinding to property having private set there is one public method SetProperty() which can be used to set Name property. So is there any way that .Net make call to this function rather then set of Name property Gaurav On Feb 27, 3:28*am, BladeWise <dany2...@xxxxxx> wrote: Quote: > Since you have a private 'set', every attempt to set the variable will > fail, wetherver your are using databinding or not. > I think the only way you can databind an object with private set > accessor is using oneway databinding (from the BindingObject to the > TextBox.Text) since there is no way for the TextBox to set the Name > property. > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: TwoWay DataBinding to property having private set On Feb 27, 3:27 pm, gauravgoyal.a...@xxxxxx wrote: Quote: > there is one public method SetProperty() which can be used to set Name > property. So is there any way that .Net make call to this function > rather then set of Name property > > Gaurav > > On Feb 27, 3:28 am, BladeWise <dany2...@xxxxxx> wrote: > Quote: > > Since you have a private 'set', every attempt to set the variable will > > fail, wetherver your are using databinding or not. > > I think the only way you can databind an object with private set > > accessor is using oneway databinding (from the BindingObject to the > > TextBox.Text) since there is no way for the TextBox to set the Name > > property. more than one parameter to set that value, otherwise there would be no need of a SetName function; unfortunally DependencyProperties, as far as I know, rely on a simple get/set with a single parameter. |
My System Specs![]() |
| | #5 (permalink) |
| | Re: TwoWay DataBinding to property having private set Before TwoWay DataBindings is established each side of the binding (dependency property) is checked to be writeable and so having a setter. This means that an exception will be raised in your case and no binding will be established. And this is fair enough. If you are intrested in binding to readonly property than you should move on to OneWay binding that perfectly suites your schema. "gauravgoyal.aqua@xxxxxx" wrote: Quote: > there is one public method SetProperty() which can be used to set Name > property. So is there any way that .Net make call to this function > rather then set of Name property > > > Gaurav > > On Feb 27, 3:28 am, BladeWise <dany2...@xxxxxx> wrote: Quote: > > Since you have a private 'set', every attempt to set the variable will > > fail, wetherver your are using databinding or not. > > I think the only way you can databind an object with private set > > accessor is using oneway databinding (from the BindingObject to the > > TextBox.Text) since there is no way for the TextBox to set the Name > > property. > > |
My System Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| wpf xml twoway data binding problem? | .NET General | |||
| Private Property, The Commons, & The War on the Middle Class | Chillout Room | |||
| WPF: Autogenerate a Private Dependency Property ? | .NET General | |||