Horizontal stackpanel to wrap DataBinded ItemsControl - wpf

I have an ItemsControl for which the ItemsSource is Binded. i coded it as per the below so that it would add the UserControl (showing the different items) to a StackPanel with a horizontal orientation that then contains a wrappanel to wrap the items inside but it is not working. All of the items are showing but they are all on one line and do not wrap to a new line when needed.
How can this code be fixed to include wrapping?
<ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto"
Grid.Column="0" Grid.Row="1">
<ItemsControl x:Name="tStack"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="1"
ItemsSource="{Binding Items.View}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel x:Name="stckPnl" Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<WrapPanel>
<Viewbox HorizontalAlignment="Left" Height="400">
<Controls1:MyItemsUserControl Padding="5"/>
</Viewbox>
</WrapPanel>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>

I have solved this issue by setting Width for WrapPanel. In below snippet i have binded WrapPanel width to its Parent Grid control named MainGrid and Path to its ActualWidth. Please see below snippet will helps you sometimes to solve your issue
<ItemsControl Name="ThemesItemControl"
Grid.Column="1"
Grid.Row="1"
ItemsSource="{Binding InstalledCollection}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
BorderThickness="0.5">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"
VerticalAlignment="Top"
Width="{Binding ElementName=MainGrid, Path=ActualWidth}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Button Width="210"
Height="260"
Margin="20"
Tag="{Binding ID}"
Command="{Binding DataContext.ThemeSelectCommand,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}">
<StackPanel>
<Image Source="{Binding TileImage}"/>
</StackPanel>
</Button>
<TextBlock Text="{Binding Title}"
FontWeight="ExtraBold"
HorizontalAlignment="Center"
FontSize="15"
FontFamily="Segoe Print"
Foreground="Red"/>
<TextBlock Text="{Binding Description}"
HorizontalAlignment="Center"
FontSize="13"
FontFamily="Segoe Print"
Foreground="Red"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

Related

Nested Scrollbar only part visible

I am unable to work out how to provide space for a nested ScrollViewer control.
I have a Border with nested ScrollViewer which contains ListView templated to render multiple DevExpress GridControl controls.
I've tried adding padding, margins etc nothing seems to work.
This Grid is there to allow me to add multiple StackPanel as they are made visible at different points. I've trimmed out the code for conciseness.
<Border BorderThickness="4" Grid.Column="0" Grid.Row="2">
<ScrollViewer VerticalScrollBarVisibility="Auto" Width="450" MinHeight="200" MaxHeight="600">
<Grid>
<StackPanel HorizontalAlignment="Center">
<ListView ItemsSource="{Binding FilterItemLists}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" view:MarginSetter.Margin="5">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"></WrapPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Width="200">
<dxlc:LayoutItem VerticalAlignment="Stretch" Padding="4">
<Label Content="{Binding Title}"/>
</dxlc:LayoutItem>
<dxlc:LayoutItem VerticalAlignment="Stretch" Padding="4" Height="200" >
<dxg:GridControl ItemsSource="{Binding Path=Items, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="None" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="IsSelected" Width="20" FixedWidth="True" Fixed="Left">
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<dxe:CheckEdit MaxWidth="20" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=DataContext.RowData.Row.IsSelected, Mode=TwoWay}"
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type dxg:GridControl}}, Path=DataContext.FilterItemSelectedCommand}"
CommandParameter="{Binding Path=RowData.Row}"
HorizontalAlignment="Center" VerticalAlignment="Center">
</dxe:CheckEdit>
</DataTemplate>
</dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
<dxg:GridColumn FieldName="Name" BestFitMode="AllRows"/>
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TableView AllowEditing="False" AutoWidth="True" ShowBandsInCustomizationForm="False" ShowBandsPanel="False"
ShowColumnHeaders="False" ShowGroupPanel="False" ShowIndicator="False" ShowSearchPanelCloseButton="False"
ShowVerticalLines="False" ShowHorizontalLines="False"/>
</dxg:GridControl.View>
</dxg:GridControl>
</dxlc:LayoutItem>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</Grid>
</ScrollViewer>
</Border>

WPF - The buttons next to each other

I have button and under him is text and these buttons are next to each other but are served under him. This is my code:
UserView.xaml:
<WrapPanel Orientation="Horizontal" HorizontalAlignment = "Left">
<ItemsControl ItemsSource = "{Binding Path = Users}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation = "Vertical">
<Button Style="{StaticResource UserButton}" Content="{Binding Name}"></Button>
<Rectangle Style="{StaticResource UserButtonStatus}"
Fill="{Binding Color}" ToolTip="{Binding Tooltip}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</WrapPanel>
MainWindow.xaml:
<StackPanel Grid.Row="0" Grid.Column="0" Orientation="Vertical">
<TextBlock Style="{StaticResource Title}">Users</TextBlock>
<view:UserView x:Name="UserView">
<view:UserView.DataContext>
<Binding Path="UserViewModel" Source="{StaticResource ServiceLocator}"/>
</view:UserView.DataContext>
</view:UserView>
</StackPanel>
Required:
Actual (wrong):
You need to overwrite the items controls panel
items control will display each item underneath each other by default.
here is the code to add make sure its inside your items control tab just like you have done with the item template:
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
So your userview.xaml will look like this:
<ItemsControl ItemsSource = "{Binding Path = Users}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation = "Vertical">
<Button Style="{StaticResource UserButton}" Content="{Binding Name}"></Button>
<Rectangle Style="{StaticResource UserButtonStatus}" Fill="{Binding Color}" ToolTip="{Binding Tooltip}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

Why my stackpanel items always aligning to vertical if even i mentioned horizantalmode in silverlight?

I wrote the below code in my page:
<StackPanel HorizontalAlignment="Left" Height="166" Margin="10,602,0,0" VerticalAlignment="Top" Width="1346" x:Name="thumbnailViewer">
<ScrollViewer
x:Name="thumbnailViewerScroller"
Padding="0"
BorderThickness="0"
VerticalScrollBarVisibility="Hidden"
HorizontalScrollBarVisibility="Hidden">
<StackPanel Orientation="Horizontal" >
<ItemsControl x:Name="UserList">
<ItemsControl.ItemTemplate>
<DataTemplate>
<!--<StackPanel Orientation="Horizontal">-->
<Image Source="{Binding imageurl}" Tag="{Binding Path=id}" Width="164" Height="150" Margin="4" Stretch="Fill"></Image>
<!--</StackPanel>-->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
</StackPanel>
Inside of the scrollviewer i mentionsed stackpanel and aligning the items as horizontal.But always i am getting the items alignment as vertical while running of the code.Please tell me how to align the items as horizontal?What was wrong in my code why items are aliging to vertical even i mentionsed Orientation="Horizontal in stackpanel.
EDIT:
<ScrollViewer
x:Name="thumbnailViewerScroller"
Padding="0"
BorderThickness="0"
VerticalScrollBarVisibility="Hidden"
HorizontalScrollBarVisibility="Hidden">
<!--<StackPanel Orientation="Horizontal" >-->
<ItemsControl x:Name="UserList">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<!--<DataTemplate>-->
<StackPanel Orientation="Horizontal">
<Image Source="{Binding imageurl}" Tag="{Binding Path=id}" Width="164" Height="150" Margin="4" Stretch="Fill"></Image>
</StackPanel>
<!--</DataTemplate>-->
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Scrollviewer>
</Stackpanel>
Use the ItemsPanel of the ItemsControl instead.
<ScrollViewer>
<ItemsControl ...>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding imageurl}"
Tag="{Binding Path=id}" Width="164" Height="150"
Margin="4" Stretch="Fill"></Image>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>

Wpf Button Width in GroupBox Header

I have the following code:
<Window.Resources>
<DataTemplate x:Key="ParameterItemTemplate">
<my:ParameterItem ParamValue="{Binding Value}" Description="{Binding Name}"/>
</DataTemplate>
</Window.Resources>
<Grid Width="Auto">
<GroupBox BorderBrush="Black"
BorderThickness="2"
Width="Auto"
HorizontalAlignment="Left"
VerticalAlignment="Top">
<GroupBox.HeaderTemplate>
<DataTemplate>
<Button Content="Header"
Width="{Binding RelativeSource={RelativeSource Self}, Path=Width}"
Height="30">
</Button>
</DataTemplate>
</GroupBox.HeaderTemplate>
<ScrollViewer HorizontalScrollBarVisibility="Auto">
<ItemsControl x:Name="Test"
ItemsSource="{Binding Items}"
ItemTemplate="{StaticResource ParameterItemTemplate}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical" Height="228"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
</GroupBox>
</Grid>
When the binded items fill my ItemsControl, the Button placed in the Header of the GroupBox does not change it's width. Do I have a binding problem?
The width of the button fits only it's content.
What do you expect if you bind the width with itself? Try
<Button Content="Header"
Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type GroupBox}}, Path=ActualWidth}"
Height="30">

Showing a collection of items inside of a ItemsControl horizontally

Here is the XAML markup:
<ScrollViewer Grid.Column="1" Grid.Row="2" HorizontalScrollBarVisibility="Disabled" Width="990">
<StackPanel Margin="50 0 0 40">
<ItemsControl x:Name="streamList" ItemsSource="{Binding StreamInformations}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10" Orientation="Horizontal" >
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ImageUrl}" Height="60" />
<StackPanel Margin="10 0 0 0" VerticalAlignment="Center">
<TextBlock Foreground="Black" Text="{Binding ChannelName}" FontSize="12" />
<TextBlock Foreground="#999" Text="{Binding PlayerName}" FontSize="10" />
<TextBlock Foreground="#999" Text="{Binding ViewCount}" FontSize="10" />
</StackPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
And this is how it looks:
I'd like these items to appear horizontally and flow down when it reaches the edge of the StackPanel.
It seems that currently, each item in my DataContext collection is creating it's own StackPanel, so this isn't what I want.
Any suggestions?
If you change the ItemsPanel template to either a WrapPanel or horizontal StackPanel, you can achieve the effect you are after...
<ItemsControl>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<!--other stuff here-->
</ItemsControl.ItemTemplate>
</ItemsControl>
In this snippet, the ItemsPanel is set to a WrapPanel oriented horizontally. The ItemTemplate would include the binding and styling you already have...

Resources