daniel
daniel

Reputation: 35733

Set scrollbar visibility of ListBox in code behind

I want to create the following XAML dynamically in my c# code:

<ListBox x:Name="galerielb" ScrollViewer.HorizontalScrollBarVisibility="Disabled"  Margin="10,0,0,10" >
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel IsItemsHost="True" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

I am stuck with the ScrollViewer. How can I set in code behind? My code so far:

string xaml = @"<ItemsPanelTemplate  xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
          xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'><WrapPanel  IsItemsHost=""True"" /></ItemsPanelTemplate>";
galerielb.ItemsPanel = System.Windows.Markup.XamlReader.Parse(xaml) as ItemsPanelTemplate;    
galeries.Children.Add(galerielb);

Upvotes: 0

Views: 478

Answers (1)

ASh
ASh

Reputation: 35730

ScrollViewer.HorizontalScrollBarVisibility is an attached DependencyProperty. ScrollViewer class has static method to set that property for any dependency object:

ScrollViewer.SetHorizontalScrollBarVisibility(galerielb, ScrollBarVisibility.Disabled);

SetValue(), defined in DependencyObject, also works:

galerielb.SetValue(ScrollViewer.HorizontalScrollBarVisibility, ScrollBarVisibility.Disabled);

Upvotes: 3

Related Questions