Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

TwoWay DataBinding to property having private set

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 02-26-2008   #1 (permalink)
gauravgoyal.aqua
Guest


 

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
Guest


 

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
Guest


 

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
Guest


 

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
Guest


 

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
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Use my custom TypeDescriptor to obtains default Value on property inXAML Property Editor of Visual Studio 2008 azerty Avalon 0 04-14-2008 06:14 AM
WPF: Autogenerate a Private Dependency Property ? Jules Winfield .NET General 4 03-06-2008 09:43 AM
Private Network becomes Public on reboot and I want it as Private. Jon Vista networking & sharing 5 01-08-2008 09:59 AM
Bug about TwoWay DataBinding & Coerce function HolaMan Avalon 0 11-21-2006 03:14 AM
Animating databinding changes ? jods Avalon 4 06-20-2006 03:21 PM


Vistax64.com 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 2005-2008

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 47 48 49 50 51