This question is locked. New answers and comments are not allowed.
Build 2009.3.1208 It looks like Visibility ContainerBinding does not work properly.
XAML Source:
| <UserControl |
| x:Class="RadTreeViewTest.MainPage" |
| xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
| xmlns:RadNavigation="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation" |
| xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls" |
| > |
| <UserControl.Resources> |
| <telerik:ContainerBindingCollection x:Key="NodeBindings"> |
| <telerik:ContainerBinding PropertyName="Visibility" Binding="{Binding Visibility, Mode=TwoWay}" /> |
| </telerik:ContainerBindingCollection> |
| <telerik:HierarchicalDataTemplate x:Key="NodeTemplate" ItemsSource="{Binding Nodes}" telerik:ContainerBinding.ContainerBindings="{StaticResource NodeBindings}"> |
| <TextBlock Text="{Binding Text}" VerticalAlignment="Center" Margin="3,0,0,0" Visibility="{Binding Visibility}" /> |
| </telerik:HierarchicalDataTemplate> |
| </UserControl.Resources> |
| <Grid> |
| <Grid.RowDefinitions> |
| <RowDefinition Height="Auto" /> |
| <RowDefinition Height="Auto" /> |
| </Grid.RowDefinitions> |
| <TextBox |
| Name="SearchTextBox" |
| Grid.Row="0" |
| Width="200" |
| TextChanged="SearchTextBox_OnTextChanged" |
| /> |
| <RadNavigation:RadTreeView |
| Name="testTreeView" |
| ItemTemplate="{StaticResource NodeTemplate}" |
| Grid.Row="1" |
| Margin="0,5,0,0" |
| Width="200" |
| Height="350" |
| BorderBrush="Black" |
| BorderThickness="1" |
| /> |
| </Grid> |
| </UserControl> |
C# Source:
| using System.Collections.Generic; |
| using System.Collections.ObjectModel; |
| using System.ComponentModel; |
| using System.Globalization; |
| using System.Windows; |
| using System.Windows.Controls; |
| namespace RadTreeViewTest |
| { |
| public partial class MainPage |
| { |
| private readonly List<ApplicationMenuNode> _nodes; |
| public MainPage() |
| { |
| InitializeComponent(); |
| _nodes = CreateSource(); |
| testTreeView.ItemsSource = _nodes; |
| } |
| #region private static CreateSource |
| private static List<ApplicationMenuNode> CreateSource() |
| { |
| List<ApplicationMenuNode> rez = new List<ApplicationMenuNode>(); |
| for (int nodeNum = 1; nodeNum < 6; nodeNum++) |
| { |
| ApplicationMenuNode node = new ApplicationMenuNode |
| { |
| Text = string.Format(CultureInfo.CurrentUICulture, "Node {0}", nodeNum), |
| }; |
| for (int subNodeNum = 1; subNodeNum < 6; subNodeNum++) |
| { |
| node.Nodes.Add(new ApplicationMenuNode |
| { |
| Text = string.Format(CultureInfo.CurrentUICulture, "Node {0}.{1}", nodeNum, subNodeNum), |
| }); |
| } |
| rez.Add(node); |
| } |
| return rez; |
| } |
| #endregion |
| #region private void SearchTextBox_OnTextChanged |
| private void SearchTextBox_OnTextChanged(object sender, TextChangedEventArgs e) |
| { |
| foreach (ApplicationMenuNode node in _nodes) |
| { |
| node.FilterByText(SearchTextBox.Text); |
| } |
| testTreeView.ExpandAll(); |
| } |
| #endregion |
| } |
| public class ApplicationMenuNode : INotifyPropertyChanged |
| { |
| #region Properties |
| public event PropertyChangedEventHandler PropertyChanged; |
| public string Text { get; set; } |
| private Visibility _visibility = Visibility.Visible; |
| public Visibility Visibility |
| { |
| get { return _visibility; } |
| set |
| { |
| if (_visibility != value) |
| { |
| _visibility = value; |
| InvokePropertyChanged(new PropertyChangedEventArgs("Visibility")); |
| } |
| } |
| } |
| private readonly ObservableCollection<ApplicationMenuNode> _nodes = new ObservableCollection<ApplicationMenuNode>(); |
| public ObservableCollection<ApplicationMenuNode> Nodes { get { return _nodes; } } |
| #endregion |
| #region private InvokePropertyChanged |
| private void InvokePropertyChanged(PropertyChangedEventArgs e) |
| { |
| PropertyChangedEventHandler Handler = PropertyChanged; |
| if (Handler != null) |
| { |
| Handler(this, e); |
| } |
| } |
| #endregion |
| #region public FilterByText |
| public bool FilterByText(string text) |
| { |
| bool isAnyChildVisible = (Nodes.Count == 0); |
| foreach (ApplicationMenuNode node in _nodes) |
| { |
| bool isChildVisibile = node.FilterByText(text); |
| isAnyChildVisible = isAnyChildVisible || isChildVisibile; |
| } |
| bool isVisible; |
| if (string.IsNullOrEmpty(text) || (Text != null && Text.ToUpper(CultureInfo.CurrentCulture).Contains(text.ToUpper(CultureInfo.CurrentCulture)))) |
| { |
| isVisible = isAnyChildVisible; |
| } |
| else |
| { |
| isVisible = ((_nodes.Count != 0) && isAnyChildVisible); |
| } |
| Visibility = isVisible ? Visibility.Visible : Visibility.Collapsed; |
| return isVisible; |
| } |
| #endregion |
| } |
| } |
Compile and run. enter ".2" in the text box and you can see empty spaces inside TreeView.
Screenshot
It looks like a bug. Is there any workaround for now and can it, please, be fixed ?