Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - TwoWay DataBinding to property having private set

 
 
Old 02-26-2008   #1 (permalink)
gauravgoyal.aqua


 
 

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 SpecsSystem Spec
Old 02-27-2008   #2 (permalink)
BladeWise


 
 

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 SpecsSystem Spec
Old 02-27-2008   #3 (permalink)
gauravgoyal.aqua


 
 

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 SpecsSystem Spec
Old 03-02-2008   #4 (permalink)
BladeWise


 
 

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.
I fear the only way is the set accessor. I suppose you need to pass
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 SpecsSystem Spec
Old 04-03-2008   #5 (permalink)
Denis Vuyka


 
 

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 SpecsSystem Spec
 

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46