Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8986781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:37:57+00:00 2026-06-15T21:37:57+00:00

I want to adjust the width or visibility of a Border or Margin that

  • 0

I want to adjust the width or visibility of a Border or Margin that is placed above a separate ListBox if the vertical scroll bar of the ListBox is collapsed.

I am trying something like this, but cannot figure out how to get to the ListBox ScrollViewer. Obviously, the Path in the DataTrigger is not correct.

<Border Width={Binding Source={x:Static SystemParameters.ScrollWidth}}">
   <Border.Style>
      <Style>
         <Setter Property="Border.Visibility" Value="Visible"/>
         <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=listBox, 
                         Path=***ScrollViewer.ComputedVerticalScrollBarVisibility***}"
                         Value="Collapsed">
               <Setter Property="Border.Visibility" Value="Collapsed"/>
            </DataTrigger>
         </Style.Triggers>
      </Style>
   </Border.Style>
</Border>

<ListBox Name="listBox" ItemsSource="{Binding MyItems}"/>

Is there a way to get to that ListBox property? If not, is there a better way to solve this problem?

Thanks much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:37:59+00:00Added an answer on June 15, 2026 at 9:37 pm

    The workaround of wrapping the ListBox in another ScrollViewer allows an all-XAML solution at the expense of a redundant ScrollViewer control (see comment in the question). In the end, my team-mate decided to go with a code-behind solution as follows.

    Here are the relevant attributes for the ListBox that has the ScrollViewer we need to access.

    <ListBox Name="_listBox" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
             Loaded="InitializeListBoxScrollViewerProperty"/>
    

    Here is the code-behind to expose the scroll viewer for use by external controls.

    private static readonly DependencyPropertyKey ListBoxScrollViewerPropertyKey =
       DependencyProperty.RegisterReadOnly("ListBoxScrollViewer", typeof(ScrollViewer),
       typeof(MyEditView), new PropertyMetadata());
    
    protected static readonly DependencyProperty ListBoxScrollViewerProperty =
       ListBoxScrollViewerPropertyKey.DependencyProperty;
    
    protected ScrollViewer ListBoxScrollViewer
    {
       get { return (ScrollViewer)GetValue(ListBoxScrollViewerProperty); }
       private set { SetValue(ListBoxScrollViewerPropertyKey, value); }
    }
    
    private void InitializeListBoxScrollViewerProperty(object sender, RoutedEventArgs args)
    {
       if (ReferenceEquals(args.OriginalSource, _listBox))
       {
          var scrollViewer = _listBox.GetFirstDescendantBreadthFirst<ScrollViewer>();
          if (scrollViewer != null)
          {
             ListBoxScrollViewer = scrollViewer;
          }
       }
    }
    

    Here are the extension methods used:

    public static class DependencyObjectExtensions
    {
       public static TDescendant GetFirstDescendantBreadthFirst<TDescendant>
          (this DependencyObject dependencyObject) where TDescendant : DependencyObject
       {
          if (dependencyObject == null) { throw new ArgumentNullException(); }
          return GetFirstDescendantBreadthFirst<TDescendant>(GetAllChildren(dependencyObject));
       }
    
       private static TDescendant GetFirstDescendantBreadthFirst<TDescendant>
          (IEnumerable<DependencyObject> descendants) where TDescendant : DependencyObject
       {
          if (!descendants.Any()) return null;
          var descendant = descendants.OfType<TDescendant>().FirstOrDefault();
          if (descendant != null) return descendant;
          return GetFirstDescendantBreadthFirst<TDescendant>(descendants.SelectMany(GetAllChildren));
       }
    
       private static IEnumerable<DependencyObject> GetAllChildren(DependencyObject dependencyObject)
       {
          return Enumerable
             .Range(0, VisualTreeHelper.GetChildrenCount(dependencyObject))
             .Select(i => VisualTreeHelper.GetChild(dependencyObject, i));
       }
    }
    

    And finally, the scroll viewer can be accessed in another part of the XAML.

    <Grid>
       <Grid.Style>
          <Style TargetType="Grid">
             <Style.Triggers>
                <DataTrigger Binding="{Binding ListBoxScrollViewer.ComputedVerticalScrollBarVisibility,
                   RelativeSource={RelativeSource AncestorType=l:MyEditView}}" Value="Visible">
                   <Setter Property="Margin" Value="{StaticResource myWidenedMargin}"/>
                </DataTrigger>
             </Style.Triggers>
          </Style>
       </Grid.Style>
    </Grid>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often find that I want an element to adjust its width to the
I want to adjust zsh so that I can tab complete: myprog <tab> using
I want a page that has two fixed-width columns on the left and a
I have two divs that have 50% width each. I want to make it
I want to adjust the width of DropDownList control in flex 4. I could
I want to embed some ruby code to adjust the width of a on
Re: http://fancyapps.com/fancybox/#docs I’ve set the option for a custom margin but want to adjust
To make sure that the text in listbox items is not truncated I want
I am working on a blog at www.tosreport.com/scripting and might want to adjust where
when my form is shown, i want to adjust the height of certain control,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.