Windows Vista Beta | WinVistaBeta.com - Message | TwoWay DataBinding to property having private set

July 05, 2008  
Subject: TwoWay DataBinding to property having private set
Group: microsoft.public.windows.developer.winfx.avalon
Date: 2/26/2008 3:48:02 PM
From: gauravgoyal.aqua@gmail.com

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

Back
Subject: Re: TwoWay DataBinding to property having private set
Group: microsoft.public.windows.developer.winfx.avalon
Date: 2/27/2008 1:28:23 AM
From: BladeWise [Email Address Protection]

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...@gmail.com wrote:
> 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


Back
Subject: Re: TwoWay DataBinding to property having private set
Group: microsoft.public.windows.developer.winfx.avalon
Date: 2/27/2008 7:27:43 AM
From: gauravgoyal.aqua@gmail.com

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=A0am, BladeWise <dany2...@gmail.com> wrote:
> 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.
>

Back
Subject: Re: TwoWay DataBinding to property having private set
Group: microsoft.public.windows.developer.winfx.avalon
Date: 3/2/2008 8:17:54 AM
From: BladeWise [Email Address Protection]

On Feb 27, 3:27 pm, gauravgoyal.a...@gmail.com wrote:
> 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...@gmail.com> wrote:
>
> > 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.

Back