Binding Visibility with ContainerBinding

4 posts, 1 answers
  1. hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 05 Jun 2009 Link to this post

    For various reasons that I don't want to go into here I need to hide one of the items in a combobox. I tried doing this with container bindings but it does not work. Binding IsSelected and IsEnabled work fine, but not Visibility (see the code below). Is this a bug, or is it not meant to work?

    <UserControl x:Class="TelerikTestProject.SilverlightControl3" 
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                 xmlns:telerikbase="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls" 
                 xmlns:telerikinput="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input" 
                 xmlns:teleriknav="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation" 
                 xmlns:local="clr-namespace:TelerikTestProject" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                 Width="400" 
                 Height="300"
        <Grid x:Name="LayoutRoot" 
              Background="White"
             
            <Grid.Resources> 
                <telerikbase:ContainerBindingCollection x:Name="UserItemBindings"
                    <telerikbase:ContainerBinding PropertyName="IsSelected" 
                                                  Binding="{Binding IsSelected, Mode=TwoWay}" /> 
                    <telerikbase:ContainerBinding PropertyName="Visibility" 
                                                  Binding="{Binding UserVisibility, Mode=TwoWay}" /> 
                    <telerikbase:ContainerBinding PropertyName="IsEnabled" 
                                                  Binding="{Binding UserIsEnabled, Mode=TwoWay}" /> 
                </telerikbase:ContainerBindingCollection> 
     
                <DataTemplate x:Key="UserItemTemplate" 
                              telerikbase:ContainerBinding.ContainerBindings="{StaticResource UserItemBindings}"
                    <TextBlock Text="{Binding FirstName}"></TextBlock> 
                </DataTemplate> 
                 
                <DataTemplate x:Key="UserItemTemplate_SelectionBox"
                    <TextBlock Text="{Binding FirstName}"></TextBlock> 
                </DataTemplate> 
     
            </Grid.Resources> 
     
            <telerikinput:RadComboBox x:Name="RCBUsers" 
                                      VerticalAlignment="Top" 
                                      HorizontalAlignment="Left" 
                                      ItemTemplate="{StaticResource UserItemTemplate}" 
                                      SelectionBoxItemTemplate="{StaticResource UserItemTemplate_SelectionBox}"
            </telerikinput:RadComboBox> 
     
        </Grid> 
    </UserControl> 
     

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Net; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Documents; 
    using System.Windows.Input; 
    using System.Windows.Media; 
    using System.Windows.Media.Animation; 
    using System.Windows.Shapes; 
    using Telerik.Windows.Controls; 
    using System.Collections.ObjectModel; 
     
    namespace TelerikTestProject 
        public partial class SilverlightControl3 : UserControl 
        { 
            public SilverlightControl3() 
            { 
                InitializeComponent(); 
     
                var users = UserInfo.Get10(); 
                users[3].IsSelected = true
                users[4].UserVisibility = Visibility.Collapsed; 
                users[4].UserIsEnabled = false
     
                RCBUsers.ItemsSource = users; 
     
                RCBUsers.SelectedItem = (from user in users where user.IsSelected select user).SingleOrDefault(); 
            } 
        } 
     
        public class UserInfo 
        { 
            public string FirstName { getset; } 
            public string LastName { getset; } 
            public int Age { getset; } 
            public bool IsSelected { getset; } 
            public Visibility UserVisibility { getset; } 
            public bool UserIsEnabled { getset; } 
     
            public UserInfo() 
            { 
            } 
     
            public static ObservableCollection<UserInfo> Get10() 
            { 
                ObservableCollection<UserInfo> users = new ObservableCollection<UserInfo>(); 
     
                for (int i = 0; i < 100; i++) 
                { 
                    users.Add(new UserInfo() 
                    { 
                        FirstName = String.Format("FirstName {0}", i), 
                        LastName = String.Format("LastName {0}", i), 
                        Age = i, 
                        UserVisibility = Visibility.Visible, 
                        UserIsEnabled = true 
                    }); 
                } 
     
                return users; 
            } 
     
            public static ObservableCollection<UserInfo> Get100() 
            { 
                ObservableCollection<UserInfo> users = new ObservableCollection<UserInfo>(); 
     
                for (int i = 0; i < 100; i++) 
                { 
                    users.Add(new UserInfo() 
                    { 
                        FirstName = String.Format("FirstName {0}", i), 
                        LastName = String.Format("LastName {0}", i), 
                        Age = i, 
                        UserVisibility = Visibility.Visible, 
                        UserIsEnabled = true 
                    }); 
                } 
     
                return users; 
            } 
        } 
     



  2. Answer
    Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 05 Jun 2009 Link to this post

    Hello Henrik,

    I suppose this is a bug in RadComboBox. I will explain a little bit more. When FilteringMode == Contains or StartsWith, RadComboBox searches through its items for items that match its Text. Those items that match are made visible, the rest are collapsed. In order to display all collapsed items when the popup is opened, RadComboBox runs such matching cycle with empty text every time the dropdown is opened. This is the expected behavior (in my opinion) when FilteringMode is not None, but when it is None, the container bindings should work and this is the case I am going to fix. I would be glad to hear your opinion about that behavior.

    Unfortunately it is too late to include the fix in the today's internal build, if this problem is not a showstopper for you, I would recommend waiting for the next internal build, next friday. Otherwise, I could provide you with a build earlier next week.

    Other possible (and probably better) solution would be to instead of hiding the items that should not be visible, to remove them from the ItemsSource of RadComboBox.

    Best wishes,
    Valeri Hristov
    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.
  3. DevCraft banner
  4. hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 05 Jun 2009 Link to this post

    That is exactly the solution I need. I will wait for the next build then, no problem. And yes, you're right, the obvious solution would be to remove the items from the itemssource. I will explain why this is not a good solution for me, and also give you an idea for a new feature for the combobox.

    I am binding my combobox against a globally cached list of items. This list is used by multiple components in the application. In some cases, when I use the list, it is mandatory for the user to select an item. In other cases it is not mandatory, and I want to provide the user with the possibility of actively selecting "None" in the combobox. Therefor I have to manually add a "None" item to my globally cached list of business objects. I give this "None" item an id of -1. Sometimes this item should be visible to the user, and sometimes not (when selection is mandatory), hence the need for hiding this comboboxitem using the containerbinding. I know I could easily LINQ out the "None" item and bind against the result of the LINQ query, but that would require code, which I'm trying to avoid.

    Now to my feature request: It would be great if the Telerik combobox could provide us with some kind of support for adding this "None" item without having to actually modify the underlying collection of business objects. After all, it is a very common scenario to have a combobox with one item on top that says "Select »" or "(None selected)" or whatever text one wants to display.

    Anyway, thanks for addressing this issue.

    Best regards,
    /Henrik


  5. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 08 Jun 2009 Link to this post

    Hi Henrik,

    Thank you for the feature request. I see it like a button in the dropdown, above the ScrollViewer with the ItemsPresenter, that has customizable text with default value something like "Select None", that will set SelectedIndex = -1 and then close the dropdown.

    I will check-in the bugfix for the Visibility bindings tomorrow, it will be available in the nightly build this friday.

    Sincerely yours,
    Valeri Hristov
    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.
Back to Top