This is a migrated thread and some comments may be shown as answers.

Binding checkboxes in xaml

4 Answers 190 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 24 Apr 2009, 08:36 PM
Hi,

I'm stuck on how to do two things with checkboxes in the tree. I am binding to an object which has many properties.

One property is an integer with values 1-5. I would like nodes that have a value of greater than one to have a checkbox beside them, but am not sure how to put this into the itemtemplate. (I can add a boolean get/set to the object if necessary).

Another property is a boolean - true or false. I would like nodes with a value of 'true' to be checked.

Is this possible to do in xaml? (or any other way!)

Thanks, James.

4 Answers, 1 is accepted

Sort by
0
Accepted
Miroslav
Telerik team
answered on 26 Apr 2009, 02:41 PM
Hi James,

Yes you can put anything you like in the ItemTemplate,

if you need different item templates for the different items, please use the ItemTemplate selector and base and include your template selection logic there. For more information, please have a look at this example:

http://demos.telerik.com/silverlight/#TreeView/HierarchicalTemplate

Also, I have attached a project that I created for another thread, but it uses a TreeView with TemplateSelector that you can have a look at.

Alternatively, if you need to show/hide these checkboxes dynamically (e.g. as a result of a user action) you can use a single template with a checkbox inside and bind the Visibility property of this checkbox to your integer property using a custom binding converter.

Hopefully this will work for you,

Regards,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
James
Top achievements
Rank 1
answered on 28 Apr 2009, 04:36 PM
Hi Miroslav,

Thanks for your answer - just what I needed. The DataTemplateSelectors are really flexible! Lots of potential to do interesting things here...

One slight problem has arisen. I have one template that looks like this.

<core:HierarchicalDataTemplate x:Key="SelectableAttributeTemplate" ItemsSource="{Binding Attributes}"  >
            <StackPanel Orientation="Horizontal">
                <CheckBox x:Name="cb" Content="{Binding AttributeName}" IsChecked="{Binding IsSelected}" Tag="{Binding}" Unchecked="cb_Unchecked" Checked="cb_Checked" />
            </StackPanel>
        </core:HierarchicalDataTemplate>

It works in the way that it should (I'm using the Tag to keep track of which item is being sent to the event handlers), but the 'Checked' event is firing when a user expands branches in the tree. I can see why this is happening, but it's problematic. Is there any way of telling the difference between a user-fired Checked event (due to them actually checking the box) and an interface-fired one (due to a node being expanded)?

Thanks for your help, James.



0
Accepted
Miroslav
Telerik team
answered on 30 Apr 2009, 09:42 AM
Hi James,

Unfortunately there is no easy way to make a distinction, you may need to use a flag to mark the events.

Looking at the code, I have two suggestions:

1. You do no need this binding Tag="{Binding}". This assigns the DataContext to the Tag property. You can always access the item straight from the DataContext property of the control (checkbox). This way the Tag is free for something else.

2. You may want to set a two way binding here: IsChecked="{Binding IsSelected, Mode=TwoWay}". This way the binding itself will update your value and you may be able to remove the Checked/Unchecked handlers.

Greetings,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
James
Top achievements
Rank 1
answered on 30 Apr 2009, 10:26 PM
Hi Miroslav,

Thanks for your answer. I can confirm that your suggestions are good - I'd already added the TwoWay binding (which is definitely needed) and had figured out a way of identifying 'unclicked' changes. I also didn't need to bind my object to the tag property.

It's working fine now and my treeview is operating how I need it to. It's a hard place to start learning databinding in silverlight though!

Thanks for your help, James.
Tags
TreeView
Asked by
James
Top achievements
Rank 1
Answers by
Miroslav
Telerik team
James
Top achievements
Rank 1
Share this question
or