Set "Visibility" is "Collapsed" in ListBox.ItemContainerStyle

2 posts, 0 answers
  1. Bill
    Bill avatar
    8 posts
    Member since:
    Dec 2012

    Posted 01 Mar 2013 Link to this post

    Hi,telerik:
    I want to show some images in the listbox with VirtualizingWrapPanel and some will not. It depends on the property(IsDefault) that i have defined.
    here is the code:

    <P><ListBox.ItemContainerStyle><BR>               
    <Style TargetType="{x:Type
    ListBoxItem}"><BR>                                     
    <Style.Triggers><BR>                       
    <DataTrigger Binding="{Binding IsDefault}"
    Value="true"><BR>                           
    <Setter Property="Visibility"
    Value="Collapsed"/><BR>                       
    </DataTrigger></P>
    <P>                       </P>
    <P>                   
    </Style.Triggers><BR>               
    </Style><BR>           
    </ListBox.ItemContainerStyle><BR></P>

    but even through the value of the "IsDefault" is set to "true",the listboxitem still display in the listbox ,and the item is blank.
    How do I do before they can get the effect I want?
    Thanks and Regards,
    Bill.

  2. Chuck
    Chuck avatar
    9 posts
    Member since:
    Jan 2013

    Posted 29 Jun 2015 in reply to Bill Link to this post

    I know this is an old post, but this might help someone. If "IsDefault" isn't a dependency property, make it one with a callback. Similar to:

    public bool ShowHidden
    {
        get { return (bool)GetValue(ShowHiddenProperty); }
        set { SetValue(ShowHiddenProperty, value); }
    }
     
    public static readonly DependencyProperty ShowHiddenProperty =
        DependencyProperty.Register("ShowHidden", typeof(bool), typeof(TileLoader), new PropertyMetadata(false, ShowHidden_Callback));
     
    private static void ShowHidden_Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TileLoader _this = d as TileLoader;
        if (!_this.dpConstructorDone) return; // prevent constructor doing a refresh (and getting an error)

                                               // if you set the property value in the constructor on load

        bool value = (bool)e.NewValue;
        // do something with value here if you want
        _this.View.CollectionView.Refresh();
    }

    Then create a collection view with a filter for your ListBox

    View.CollectionView = CollectionViewSource.GetDefaultView(View.Images);
    View.CollectionView.Filter = oImage =>
    {
        var image = (ImageDTO)oImage;
        if (image.IsHidden && !ShowHidden) return false;
        if (string.IsNullOrWhiteSpace(Search))
            return true;
        return image.Name.ToLower().Contains(Search.ToLower());
    };

    I also have a Search textbox, so my filter includes that. So when you refresh your collection view, Then you won't be displaying your hidden items (and there won't be blank boxes either.)

  3. UI for WPF is Visual Studio 2017 Ready
Back to Top