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

How to bind to 'IsChecked' property

6 Answers 700 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Roman
Top achievements
Rank 1
Roman asked on 25 Dec 2010, 08:49 PM
I bind ItemsSource property to collection of elements that have 'IsChecked' property. How to bind 'RadTreeViewItem.IsChecked' property to 'MyElement.IsChecked'?

It is needed because I set IsVirtualizing="True" and thus I have to store whether item is checked in separate place.

6 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 27 Dec 2010, 12:29 PM
Hello Roman,

You can use the ItemContainerStyle property of the RadTreeView and set a style targeting RadTreeViewItem. You can examine this article showing how to bind the IsSelected property. Binding the IsChecked should be the same. Feel free to ask if you need further assistance.
Regards,
Petar Mladenov
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
Roman
Top achievements
Rank 1
answered on 27 Dec 2010, 03:10 PM
I wrote the following style:   
<t:RadTreeView.ItemContainerStyle>
    <Style TargetType="t:RadTreeViewItem">                               
        <Setter Property="IsChecked" Value="{Binding Path=IsChecked, Mode=TwoWay}" />
    </Style>
</t:RadTreeView.ItemContainerStyle>
 
But bindings seems to work only one way. Also when I set
IsVirtualizing="True" t:TreeViewPanel.VirtualizationMode="Recycling"
Properties binding works only for visible elements, when I scroll down all new elements are unchecked.

Here is full XAML code:
<Window x:Class="WpfApplication4.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel>
        <StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
            <Button Content="Check All" Click="CheckAll_Click" Margin="6" Padding="4, 0" />
            <Button Content="Uncheck All" Click="UncheckAll_Click" Margin="6" Padding="4, 0" />
            <Button Content="Count" Click="Count_Click"  Margin="6" Padding="4, 0" />
            <TextBlock Name="label" Text="Count" VerticalAlignment="Center" />
        </StackPanel>
        <t:RadTreeView x:Name="tree" IsOptionElementsEnabled="True" ItemsOptionListType="CheckList" IsVirtualizing="True" t:TreeViewPanel.VirtualizationMode="Recycling">
        <!--t:RadTreeView x:Name="tree" IsOptionElementsEnabled="True" ItemsOptionListType="CheckList"-->
            <t:RadTreeView.ItemContainerStyle>
                <Style TargetType="t:RadTreeViewItem">
                    <Setter Property="IsChecked" Value="{Binding Path=IsChecked, Mode=TwoWay}" />
                </Style>
            </t:RadTreeView.ItemContainerStyle>
        </t:RadTreeView>
    </DockPanel>
</Window>

And backing code:
using System;
using System.Linq;
using System.Windows;
using System.Collections.ObjectModel;
using System.ComponentModel;
 
namespace WpfApplication4
{
    public partial class MainWindow : Window
    {
        ObservableCollection<MyItem> list = new ObservableCollection<MyItem>();
        public MainWindow()
        {
            InitializeComponent();
            for (int i = 0; i < 100; i++)
                list.Add(new MyItem() { Text = "item " + i, IsChecked = false });
            tree.ItemsSource = list;
        }
 
        private void CheckAll_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in list)
                item.IsChecked = true;
        }
 
        private void UncheckAll_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in list)
                item.IsChecked = false;
        }
 
        private void Count_Click(object sender, RoutedEventArgs e)
        {
            label.Text = list.Where(a => a.IsChecked).Count().ToString();
        }
    }
 
    public class MyItem : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
 
        public string Text { get; set; }
        private bool isChecked;
        public bool IsChecked
        {
            get { return isChecked; }
            set
            {
                isChecked = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
            }
        }
 
        public override string ToString()
        {
            return Text;
        }
    }
}
0
Petar Mladenov
Telerik team
answered on 29 Dec 2010, 05:39 PM
Hello Roman,

Unfortunately this appears to be a bug in the RadTreeView. The good new is that there is an elegant workaround using converter and CheckState property of the RadTreeViewItem. You can check out this blog post for more info and examine the attached solution accordingly.
You can also track the progress and vote the issue you`ve encountered here.
I also updated your telerik points.
Feel free to ask if you need further into.Thank you for your cooperation.

Best wishes,
Petar Mladenov
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
Vladislav
Telerik team
answered on 10 May 2011, 03:18 PM
Hello Roman,

We want to inform you, that this issue is already fixed.
You can check it in our latest internal build (released on 2 of May).
Any feedback/comments on the subject are highly appreciated.

Kind regards,
Vladislav
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Christian
Top achievements
Rank 1
answered on 16 Mar 2016, 01:10 PM

Hi @ll admins,

 

I am using Telerik for WPF (2016.1.112) and it seems the bug already exists.

Also: initially null is interpreted as false.

 

Best regards

Christian

0
Martin Ivanov
Telerik team
answered on 21 Mar 2016, 08:52 AM
Hi Christian,

There are known issues in the checkbox support for RadTreeView in a data binding scenario. This is why I recommend you to implement custom checkbox logic as demonstrated in the Implement a Tri-State CheckBox logic using MVVM help article. I hope this solution works for you.

Regards,
Martin
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
TreeView
Asked by
Roman
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Roman
Top achievements
Rank 1
Vladislav
Telerik team
Christian
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or