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 > .NET General

Vista Tutorial - databinding to 3-state checkbox?

Reply
 
Old 07-02-2009   #1 (permalink)
dave
Guest


 
 

databinding to 3-state checkbox?

I am binding a 3-state checkbox to an XElement:

<CheckBox Content="Summary" IsChecked="{Binding
Path=Element[StatusSummary].Value, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" IsThreeState="True" IsEnabled="False"
Margin="0,79,0,0"/>

it properly reports True and False when it is checked or unchecked. but it
retains the previous value when in the intermediate state. it would seem to
not be firing the propertychanged event to update the xelement when
transitioning to the indeterminate state. Is there a way to bind to get back
the null for indeterminate?

My System SpecsSystem Spec
Old 07-02-2009   #2 (permalink)
John Bundy
Guest


 
 

RE: databinding to 3-state checkbox?

haven't tried it as you have below, but i tried to check this intuitively
with a checkchanged event updating a label.
Me.Label1.Text = Me.CheckBox1.CheckState
i found that the indeterminate value did not fire an event with then
CheckBox1_CheckedChanged event, it did work with CheckBox1_CheckStateChanged.
So you might try checking against that event instead
--
-John
Please rate when your question is answered to help us and others know what
is helpful.


"dave" wrote:
Quote:

> I am binding a 3-state checkbox to an XElement:
>
> <CheckBox Content="Summary" IsChecked="{Binding
> Path=Element[StatusSummary].Value, Mode=TwoWay,
> UpdateSourceTrigger=PropertyChanged}" IsThreeState="True" IsEnabled="False"
> Margin="0,79,0,0"/>
>
> it properly reports True and False when it is checked or unchecked. but it
> retains the previous value when in the intermediate state. it would seem to
> not be firing the propertychanged event to update the xelement when
> transitioning to the indeterminate state. Is there a way to bind to get back
> the null for indeterminate?
My System SpecsSystem Spec
Old 07-02-2009   #3 (permalink)
dave
Guest


 
 

RE: databinding to 3-state checkbox?

I know the events for indeterminate state transitions are different, but in
the databinding you just get to specify the property you want to bind to and
the trigger mechanism for updating the source. Unfortunately you can't
specify the event to do the update unless you add your own event handler and
use the manual method to call updatesource. It appears that the trigger
mechanism doesn't recognize transitions to indeterminate, only to checked or
unchecked.

"John Bundy" wrote:
Quote:

> haven't tried it as you have below, but i tried to check this intuitively
> with a checkchanged event updating a label.
> Me.Label1.Text = Me.CheckBox1.CheckState
> i found that the indeterminate value did not fire an event with then
> CheckBox1_CheckedChanged event, it did work with CheckBox1_CheckStateChanged.
> So you might try checking against that event instead
> --
> -John
> Please rate when your question is answered to help us and others know what
> is helpful.
>
>
> "dave" wrote:
>
Quote:

> > I am binding a 3-state checkbox to an XElement:
> >
> > <CheckBox Content="Summary" IsChecked="{Binding
> > Path=Element[StatusSummary].Value, Mode=TwoWay,
> > UpdateSourceTrigger=PropertyChanged}" IsThreeState="True" IsEnabled="False"
> > Margin="0,79,0,0"/>
> >
> > it properly reports True and False when it is checked or unchecked. but it
> > retains the previous value when in the intermediate state. it would seem to
> > not be firing the propertychanged event to update the xelement when
> > transitioning to the indeterminate state. Is there a way to bind to get back
> > the null for indeterminate?
My System SpecsSystem Spec
Old 07-02-2009   #4 (permalink)
dave
Guest


 
 

RE: databinding to 3-state checkbox?

More info... i was missing something, there is an error generated when it
tries to set the null value back to the xml:

System.Windows.Data Error: 8 : Cannot save value from target back to source.
BindingExpression:Path=Element[StatusSummary].Value; DataItem='XElement'
(HashCode=51920462); target element is 'CheckBox' (Name=''); target property
is 'IsChecked' (type 'Nullable`1')
ArgumentNullException:'System.ArgumentNullException: Value cannot be null.
Parameter name: value
at System.Xml.Linq.XElement.set_Value(String value)
at
System.Xml.Linq.ComponentModel.XElementValuePropertyDescriptor.SetValue(Object component, Object value)
at MS.Internal.Data.PropertyPathWorker.SetValue(Object item, Object value)
at MS.Internal.Data.ClrBindingWorker.UpdateValue(Object value)
at System.Windows.Data.BindingExpression.UpdateSource(Object value)'


So it looks like the set_Value for a string can't handle the null for the
checkbox in tristate mode.





"dave" wrote:
Quote:

> I know the events for indeterminate state transitions are different, but in
> the databinding you just get to specify the property you want to bind to and
> the trigger mechanism for updating the source. Unfortunately you can't
> specify the event to do the update unless you add your own event handler and
> use the manual method to call updatesource. It appears that the trigger
> mechanism doesn't recognize transitions to indeterminate, only to checked or
> unchecked.
>
> "John Bundy" wrote:
>
Quote:

> > haven't tried it as you have below, but i tried to check this intuitively
> > with a checkchanged event updating a label.
> > Me.Label1.Text = Me.CheckBox1.CheckState
> > i found that the indeterminate value did not fire an event with then
> > CheckBox1_CheckedChanged event, it did work with CheckBox1_CheckStateChanged.
> > So you might try checking against that event instead
> > --
> > -John
> > Please rate when your question is answered to help us and others know what
> > is helpful.
> >
> >
> > "dave" wrote:
> >
Quote:

> > > I am binding a 3-state checkbox to an XElement:
> > >
> > > <CheckBox Content="Summary" IsChecked="{Binding
> > > Path=Element[StatusSummary].Value, Mode=TwoWay,
> > > UpdateSourceTrigger=PropertyChanged}" IsThreeState="True" IsEnabled="False"
> > > Margin="0,79,0,0"/>
> > >
> > > it properly reports True and False when it is checked or unchecked. but it
> > > retains the previous value when in the intermediate state. it would seem to
> > > not be firing the propertychanged event to update the xelement when
> > > transitioning to the indeterminate state. Is there a way to bind to get back
> > > the null for indeterminate?
My System SpecsSystem Spec
Old 07-02-2009   #5 (permalink)
dave
Guest


 
 

RE: databinding to 3-state checkbox?

ok, found it... this article describes the problem and fix:
http://code.msdn.microsoft.com/wpfdatabindingnull
just made a nullable bool to string converter to go from null<->"" and it is
happy.

"dave" wrote:
Quote:

> More info... i was missing something, there is an error generated when it
> tries to set the null value back to the xml:
>
> System.Windows.Data Error: 8 : Cannot save value from target back to source.
> BindingExpression:Path=Element[StatusSummary].Value; DataItem='XElement'
> (HashCode=51920462); target element is 'CheckBox' (Name=''); target property
> is 'IsChecked' (type 'Nullable`1')
> ArgumentNullException:'System.ArgumentNullException: Value cannot be null.
> Parameter name: value
> at System.Xml.Linq.XElement.set_Value(String value)
> at
> System.Xml.Linq.ComponentModel.XElementValuePropertyDescriptor.SetValue(Object component, Object value)
> at MS.Internal.Data.PropertyPathWorker.SetValue(Object item, Object value)
> at MS.Internal.Data.ClrBindingWorker.UpdateValue(Object value)
> at System.Windows.Data.BindingExpression.UpdateSource(Object value)'
>
>
> So it looks like the set_Value for a string can't handle the null for the
> checkbox in tristate mode.
>
>
>
>
>
> "dave" wrote:
>
Quote:

> > I know the events for indeterminate state transitions are different, but in
> > the databinding you just get to specify the property you want to bind to and
> > the trigger mechanism for updating the source. Unfortunately you can't
> > specify the event to do the update unless you add your own event handler and
> > use the manual method to call updatesource. It appears that the trigger
> > mechanism doesn't recognize transitions to indeterminate, only to checked or
> > unchecked.
> >
> > "John Bundy" wrote:
> >
Quote:

> > > haven't tried it as you have below, but i tried to check this intuitively
> > > with a checkchanged event updating a label.
> > > Me.Label1.Text = Me.CheckBox1.CheckState
> > > i found that the indeterminate value did not fire an event with then
> > > CheckBox1_CheckedChanged event, it did work with CheckBox1_CheckStateChanged.
> > > So you might try checking against that event instead
> > > --
> > > -John
> > > Please rate when your question is answered to help us and others know what
> > > is helpful.
> > >
> > >
> > > "dave" wrote:
> > >
> > > > I am binding a 3-state checkbox to an XElement:
> > > >
> > > > <CheckBox Content="Summary" IsChecked="{Binding
> > > > Path=Element[StatusSummary].Value, Mode=TwoWay,
> > > > UpdateSourceTrigger=PropertyChanged}" IsThreeState="True" IsEnabled="False"
> > > > Margin="0,79,0,0"/>
> > > >
> > > > it properly reports True and False when it is checked or unchecked. but it
> > > > retains the previous value when in the intermediate state. it would seem to
> > > > not be firing the propertychanged event to update the xelement when
> > > > transitioning to the indeterminate state. Is there a way to bind to get back
> > > > the null for indeterminate?
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Welcome Center - can't get rid of it (checkbox is missing!) Vista General
Checkbox to never see this pop-up again. Vista account administration


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