Vertical align listbox to bottom - c#

I need to layout my listbox like on this picture
I have tried everything to do that both for listbox and longlistselector..
<ListBox WP7Panels:DockPanel.Dock="Bottom" Name="MsgControlsList"
ItemsSource="{Binding}"
Height="600"
HorizontalContentAlignment="Stretch"
VerticalAlignment="Bottom" VerticalContentAlignment="Bottom">
<ListBox.Style>
<Style TargetType="ListBox">
<Setter Property="VerticalAlignment"
Value="Bottom" />
<Setter Property="VerticalContentAlignment"
Value="Bottom" />
</Style>
</ListBox.Style>
<ListBox.ItemTemplate>
<DataTemplate>
<WP7Panels:DockPanel LastChildFill="True">
<HistoryClasses:HistoryElementTemplate WP7Panels:DockPanel.Dock="Bottom" VerticalAlignment="Bottom" DataContext="{Binding}" HorizontalContentAlignment="Stretch"/>
</WP7Panels:DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</WP7Panels:DockPanel>
..but still I have top vertical alignment of the listbox.
Any thoughts, please?

If you don't set the height of your listbox the items are on the bottom of the screen.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="600"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="Test"/>
<ListBox Grid.Row="1" VerticalAlignment="Bottom">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="Item"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock Grid.Row="2" Text="Test"/>
</Grid>

Related

XAML : expand expander when an item in the group is selected

Is it possible for a expander to check for the IsExpanded Property if an item inside the group of the expander is selected then this expander is expanded so IsExpanded will be true? Because if i set it to true by default all expanders will be expanded at that's not the goal.
<controls:CommandListBox Grid.IsSharedSizeScope="True"
Visibility="{Binding IsComponentOrSystemPropertySearch, Converter={StaticResource BooleanToVisibilityConverter}}"
Grid.Column="2"
HorizontalAlignment="Stretch"
SelectedItem="{Binding SelectedResult}"
ToolbarItems="{Binding ToolBarItems}"
ItemsSource="{Binding SearchResults, UpdateSourceTrigger=PropertyChanged}">
<ItemsControl.Resources>
<ResourceDictionary>
<Style x:Key="GroupHeaderStyle"
TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander x:Name="exp" IsExpanded="False" Margin="0,0,0,30" >
<Expander.Header >
<TextBlock Text="{Binding Path=Name}" FontSize="18" Padding="0" Margin="0"/>
</Expander.Header>
<Grid Margin="20 0 0 0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ContentPresenter Grid.Column="0"
Grid.Row="0"
Content="{Binding Path=Items[0].DisplayRepresentation.Header}"/>
<ItemsPresenter Grid.Column="0"
Grid.Row="1"/>
</Grid>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</ItemsControl.Resources>
<ItemsControl.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
<GroupStyle.Panel>
<ItemsPanelTemplate>
<DataGridRowsPresenter />
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</ItemsControl.GroupStyle>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:DeluxePropertySearchResult}">
<ContentPresenter Content="{Binding DisplayRepresentation}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</controls:CommandListBox>

Adding dynamically created components to a WPF grid

I have a WPF grid, which needs to have RadioButtons arranged in 3 columns. The radio buttons represent each of a category, which is evaluated by the model. I have also created a property of type UIElementCollection in the ViewModel. Now the challenge is to arrange them in the grid i.e, assigning the row column values to each.
MainWindow xaml :
<Window x:Class="WPFApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFApplication"
xmlns:ViewModels="clr-namespace:WPFApplication.ViewModels"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Width="auto">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<!-- The problem with this expander is that the rows are not dynamically added.
Also, all radio buttons are selectable.-->
<Expander Name="ExpanderProductCategory" Header="Product Categories" HorizontalAlignment="Left" Height="auto" Grid.Row="0" Grid.Column="0" Margin="0,0,0,0" VerticalAlignment="Top" Width="auto">
<Expander.DataContext>
<ViewModels:ProductCategoriesViewModel/>
</Expander.DataContext>
<ItemsControl ItemsSource="{Binding ProductCategories}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid x:Name="gridRegions" Margin="5,10,0,0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="{x:Type FrameworkElement}">
<Setter Property="Grid.Row" Value="{Binding RowIndex}"/>
<Setter Property="Grid.Column" Value="{Binding ColumnIndex}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding Name }" Grid.Row="{Binding RowIndex}" Grid.Column="{Binding ColumnIndex}" Margin="0,0,5,5" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Expander>
<!-- The problem with this expander is that all radio buttons are selectable-->
<Expander Name="ExpanderProductCategory2" Header="Product Categories Uniform Grid" HorizontalAlignment="Left" Height="auto" Grid.Row="1" Grid.Column="0" Margin="0,0,0,0" VerticalAlignment="Top" Width="auto">
<Expander.DataContext>
<ViewModels:ProductCategoriesViewModel/>
</Expander.DataContext>
<ItemsControl ItemsSource="{Binding ProductCategories}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid>
</UniformGrid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="{x:Type FrameworkElement}">
<Setter Property="Grid.Row" Value="{Binding RowIndex}"/>
<Setter Property="Grid.Column" Value="{Binding ColumnIndex}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding Name}" Margin="0,0,5,5" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Expander>
</Grid>
</Window>
ViewModel code :
namespace WPFApplication.ViewModels
{
class ProductCategoriesViewModel
{
private ProductCategory[] _productCategories = { new ProductCategory( "Home Appliances",0,0),
new ProductCategory( "Personal Care",1,0),
new ProductCategory("Groceries",2,0),
new ProductCategory( "Apparels",0,1),
new ProductCategory("Stationary",1,1),
new ProductCategory("Toys",2,1),
new ProductCategory("Kitchenware",0,2) };
public ProductCategory[] ProductCategories
{
get { return _productCategories; }
}
private string[] _productCategoriesString = { "Home Appliances",
"Personal Care",
"Groceries",
"Apparels",
"Stationary",
"Toys",
"Kitchenware"};
public string[] ProductCategoriesString
{
get { return _productCategoriesString; }
}
}
}
Please suggest me the right way to handle this case in WPF and MVVM.

Custom schedule for one day XAML

I'm trying to create a custom schedule for only one day( a single column) where the elements of the schedule are binded to an ObservableCollection :
public ObservableCollection<Content> ContentsToTransfer { get; set; }
For now I'm having such code :
<Grid HorizontalAlignment="Stretch" Margin="430,52,444,0" x:Name="grid1" VerticalAlignment="Stretch" Width="Auto" OpacityMask="Black" Opacity="1" Background="#FFC2ECEC" ShowGridLines="False" >
<Grid.Resources>
<Style x:Key="VerticalSeparatorStyle"
TargetType="{x:Type Separator}"
BasedOn="{StaticResource {x:Type Separator}}">
<Setter Property="Margin" Value="0,0,0,0"/>
<Setter Property="LayoutTransform">
<Setter.Value>
<TransformGroup>
<TransformGroup.Children>
<TransformCollection>
<RotateTransform Angle="90"/>
</TransformCollection>
</TransformGroup.Children>
</TransformGroup>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Separator Grid.ColumnSpan="7" HorizontalAlignment="Stretch" Margin="0,132" x:Name="separator4" VerticalAlignment="Stretch" Width="Auto" Grid.Row="3" Grid.RowSpan="2" Background="Aqua" HorizontalContentAlignment="Stretch" Foreground="Aqua" OpacityMask="Aqua" />
<Separator HorizontalAlignment="Stretch" Margin="0,132" x:Name="separator2" VerticalAlignment="Stretch" Width="Auto" Background="Aqua" Grid.ColumnSpan="7" Grid.Row="1" Grid.RowSpan="2" HorizontalContentAlignment="Stretch" Foreground="Aqua" />
<Separator HorizontalAlignment="Stretch" x:Name="separator1" VerticalAlignment="Stretch" Background="Aqua" Grid.Row="2" Grid.ColumnSpan="7" Margin="0,129" Grid.RowSpan="2" HorizontalContentAlignment="Stretch" Foreground="Aqua" />
<Separator HorizontalAlignment="Stretch" Margin="0,128" x:Name="separator3" VerticalAlignment="Stretch" Background="Aqua" Grid.ColumnSpan="7" Grid.RowSpan="2" Width="Auto" HorizontalContentAlignment="Stretch" Foreground="Aqua" />
<Rectangle Height="Auto" HorizontalAlignment="Stretch" x:Name="rectangle1" Stroke="Aqua" VerticalAlignment="Stretch" Width="Auto" Grid.RowSpan="5" />
</Grid>
The thing I want to do now is to bind the ObservableCollection to that grid so each time a content is added to the ObservableCollection it appears on the grid as well.
I also want to add the behavior of a scheduler to it.
Can you help me out?
Am I doing it right?
Is there a best solution than doing that?
It is not possible to bind a collection to a Grid, as the grid is not an ItemsControl. You can however, bind the collection to a ListBox, or a ListView, or a WrapPanel and so on. In this scenario, I'd recommend using an ItemsControl, where the ItemsPanel is a StackPanel, here's how:
<ItemsControl ItemsSource="{Binding ContentsToTransfer}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- Your Template Here -->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Inside your DataTemplate, you simply need to define what each Content will look like. For more information on DataTemplate, see the documentation

Vertically aligning the contents of a ListBox in a Windows Store app

I have a Windows Store App with a ListBox which contains Grids of variable height. I can't get these to align at the top. I've tried every combo of VerticalAlignment and VerticalContentAlignment I can think of, and they're always centered vertically.
Here's my XAML
<Page
x:Class="MyApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid Margin="50,50,50,50" HorizontalAlignment="Stretch" Width="Auto">
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="80"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="" Margin="12,0,0,0" Style="{StaticResource HeaderTextStyle}" />
<Grid Grid.Row="1" Margin="30,0,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"></ColumnDefinition>
<ColumnDefinition Width="15"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="15"></ColumnDefinition>
<ColumnDefinition Width="1*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" x:Name="bxAddItem" Width="300" Height="28" Margin="0,0,0,0" Text="Item text" KeyUp="bxAddItem_KeyUp" GotFocus="bxAddItem_GotFocus" LostFocus="bxAddItem_LostFocus"></TextBox>
<ComboBox Grid.Column="2" x:Name="comboList" SelectionChanged="comboList_SelectionChanged" Width="100" Height="32">
<ComboBoxItem Tag="0">No List</ComboBoxItem>
</ComboBox>
<Button Grid.Column="4" x:Name="btnAddItem" Click="AddItem_Click" HorizontalAlignment="Left" Background="#45000000" Height="34">Add an Item</Button>
</Grid>
<ScrollViewer HorizontalScrollBarVisibility="Visible" Grid.Row="2">
<ListBox Width="Auto" VerticalContentAlignment="Top" ItemsSource="{Binding ToDoLists}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="Beige">
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Title}"></TextBlock>
<ListBox Grid.Row="1" Grid.Column="0" Name="ToDoList" ItemsSource="{Binding Path=Items}"
Margin="24,0,0,0" Height="Auto" MinWidth="200" VerticalAlignment="Stretch"
BorderBrush="BlueViolet" SelectionChanged="ToDoList_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding ItemName}"></TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ListBox>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel VerticalAlignment="Top" Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ListBox>
</ScrollViewer>
</Grid>
</Grid>
</Page>
EDIT:
Sheridan's answer does not work for me. There may be something else on the page that's causing the issue.
I've updated the question to include the entire page code.
EDIT 2:
This post was incorrectly tagged as WPF when in fact the issue is in a Windows Store app. This code works perfectly in WPF, hence the confusion. However it does NOT work in a Windows Store app
Have you tried setting the ItemContainerStyle to VerticalAlignment="Top"
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="VerticalAlignment" Value="Top"></Setter>
</Style>
</ListBox.ItemContainerStyle>

Updating listview height to fill new form content (c#)

I have a form with a border which contains a listview, within my c# code the height of the border is changed depending on a value. Right now the border height changes with no problem but how can I update the listview to have the same height as the border? Here is my xml:
<DataTemplate x:Key="PackageTemplate">
<Border x:Name="PackageBorder" BorderBrush="Black" BorderThickness="2" Margin="10" Padding="0" Width="100" >
<Border.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Status}" Value="1">
<Setter Property="Border.Background" Value="#FF999696"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Status}" Value="0">
<Setter Property="Border.Background" Value="#FFE4E4E4"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Layout}" Value="0">
<Setter Property="Border.Height" Value="100"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Layout}" Value="1">
<Setter Property="Border.Height" Value="200"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="70"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ListView Grid.Row="0" Grid.Column="0" Background="{x:Null}" x:Name="List" ItemsSource="{Binding Path=Collection}" ItemTemplate="{DynamicResource ChipTemplate}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" BorderThickness="0" BorderBrush="{x:Null}" Foreground="Black" VerticalAlignment="Top" Width="90" >
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" VerticalAlignment="Center" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
<Label Grid.Row="1" Grid.Column="0" Content="{Binding Path=Location}" FontSize="15" FontFamily="Arial" Foreground="Black" Background="{x:Null}" VerticalAlignment="Bottom" HorizontalAlignment="Left"></Label>
</Grid>
</Border>
</DataTemplate>
In the Xaml you have set the ListView Height to 70 by setting <RowDefinition Height="70"/> and the Width to 90, the ListView will not get any bigger then that, you will need to set <RowDefinition Height="70*"/> allow it to grow in height and remove the Width="90", or perhaps use DockPanel.
<DockPanel>
<Label DockPanel.Dock="Bottom" Content="{Binding Path=Location}" FontSize="15" FontFamily="Arial" Foreground="Black" />
<ListView x:Name="List" ItemsSource="{Binding Path=Collection}" ItemTemplate="{DynamicResource ChipTemplate}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" BorderThickness="0" BorderBrush="{x:Null}" Foreground="Black" >
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" VerticalAlignment="Center" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</DockPanel>

Categories