How to bind to 'IsChecked' property

7 posts, 0 answers
  1. Roman
    Roman avatar
    8 posts
    Member since:
    Nov 2010

    Posted 25 Dec 2010 Link to this post

    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.
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 27 Dec 2010 Link to this post

    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
  3. UI for WPF is Visual Studio 2017 Ready
  4. Roman
    Roman avatar
    8 posts
    Member since:
    Nov 2010

    Posted 27 Dec 2010 Link to this post

    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;
            }
        }
    }
  5. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 29 Dec 2010 Link to this post

    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
  6. Vladislav
    Admin
    Vladislav avatar
    276 posts

    Posted 10 May 2011 Link to this post

    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
  7. Christian
    Christian avatar
    2 posts
    Member since:
    Sep 2015

    Posted 16 Mar Link to this post

    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

  8. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 21 Mar Link to this post

    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.
Back to Top
UI for WPF is Visual Studio 2017 Ready