Wpf Button Width in GroupBox Header - wpf

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">

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>

ScrollViewer content is scrolling over other parts of window

I have the following as part of my XAML:
<DockPanel>
<ToolBar Name="toolbar1" DockPanel.Dock="Top" Height="41" Background="#FFA5D95A">
//other controls
</ToolBar>
<ScrollViewer>
<ListBox Name="listBox1" ItemsSource="{Binding ElementName=This, Path=Items}"
ItemTemplateSelector="{StaticResource entryItemTemplateSelector}">
<ListBox.Template>
<ControlTemplate>
<WrapPanel IsItemsHost="True" />
</ControlTemplate>
</ListBox.Template>
</ListBox>
</ScrollViewer>
</DockPanel>
I want the scrool bar to move the items in the ListBox just to the top of the WrapPanel, and then to stop showing them when they get to the top; in stead, these items scroll into the ToolBar at the top.
What am I doing wrong?
Note: Haing been asked, here are the DataTemplates I currently have; EntryItemHost is a type derived from WindowsFormsHost.
<DataTemplate x:Key="folderTemplate">
<my:EntryItemHost Item="{Binding}"
Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:MainWindow, AncestorLevel=1}, Path=Background}"
DoubleClick="FolderDoubleClick" />
</DataTemplate>
<DataTemplate x:Key="imageTemplate">
<my:EntryItemHost Item="{Binding}"
Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:MainWindow, AncestorLevel=1}, Path=Background}" />
</DataTemplate>
Instead of setting ListBox Template set the ItemsPanel and remove the ScrollViewer
sample
<DockPanel>
<ToolBar Name="toolbar1"
DockPanel.Dock="Top"
Height="41"
Background="#FFA5D95A">
//other controls
</ToolBar>
<ListBox Name="listBox1" ItemsSource="{Binding ElementName=This, Path=Items}"
ItemTemplateSelector="{StaticResource entryItemTemplateSelector}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType=ContentPresenter}}" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</DockPanel>
notice that I have replaced the template with ItemsPanel and restricted the width to the listbox
I made this based on the assumptions, let me know is this not what you are looking for

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>

Horizontal stackpanel to wrap DataBinded ItemsControl

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>

Resources