Grid doesn't stretch inside ListPicker item - silverlight

I have Grid inside ListPicker full mode item. Grid has two columns. First column should be left-aligned and second right-aligned.
Unfortunately this template doesn't work as expected:
<DataTemplate x:Name="ListFullModeItemTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Name}" />
<TextBlock Grid.Column="1" Text="{Binding Description}" HorizontalAlignment="Right" />
</Grid>
</DataTemplate>
Second TextBlock doesn't align to right.
When I set Grid Width property to specific value i.e.
<Grid Width="700">
...
</Grid>
then it works, but I can't do it because user can rotate phone to Portrait/Landscape.
Any ideas?
EDIT:
I also had the same problem in ListBox.
I fixed it by adding:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
I can't use this in ListPicker because it doesn't have ItemContainerStyle element.

set your grid 's width.
and change it by +=:
OrientationChanged += new EventHandler(SecondPage_OrientationChanged);

try adding TextAlignment="Right"

Related

How can I make align all the textboxes align in same vertical position in wpf?

Hi i created lable's and text boxs in wpf but text boxes are started different position How can I make align all the textboxes align in same vertical position?
<DockPanel LastChildFill="True"
HorizontalAlignment="Stretch">
<Label DockPanel.Dock="Left"
Style="{DynamicResource EditorHLabelNoIdentStyle}" Content="{x:Static r:Resources.LABEL_POSITION}"/>
<TextBox ToolTip="{x:Static r:Resources.LABEL_POSITION_TIP}"
Style="{DynamicResource EditorTextBoxStyle}" Text="{Binding XPath=nameofthedataset, Mode=TwoWay}"/>
</DockPanel>
<DockPanel LastChildFill="True"
HorizontalAlignment="Stretch">
<Label DockPanel.Dock="Left"
Style="{DynamicResource EditorHLabelNoIdentStyle}" Content="{x:Static r:Resources.LABEL_POSITION}"/>
<TextBox ToolTip="{x:Static r:Resources.LABEL_POSITION_TIP}"
Style="{DynamicResource EditorTextBoxStyle}" Text="{Binding XPath=keywords, Mode=TwoWay}"/>
</DockPanel>
Use a Grid with two ColumnDefinitions and a RowDefinition for each row and then set the Grid.Row and Grid.Column attached properties
of each element to specify its position in the Grid:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label DockPanel.Dock="Left" Style="{DynamicResource EditorHLabelNoIdentStyle}" Content="{x:Static r:Resources.LABEL_POSITION}"/>
<TextBox Grid.Column="1" ToolTip="{x:Static r:Resources.LABEL_POSITION_TIP}" Style="{DynamicResource EditorTextBoxStyle}"
Text="{Binding XPath=nameofthedataset, Mode=TwoWay}"/>
<Label Grid.Row="1" DockPanel.Dock="Left" Style="{DynamicResource EditorHLabelNoIdentStyle}" Content="{x:Static r:Resources.LABEL_POSITION}"/>
<TextBox Grid.Row="1" Grid.Column="1" ToolTip="{x:Static r:Resources.LABEL_POSITION_TIP}"
Style="{DynamicResource EditorTextBoxStyle}" Text="{Binding XPath=keywords, Mode=TwoWay}"/>
</Grid>
With a number of rows, setting labels or texblocks and textboxes to row and column quickly gets a bit tedious.
You could instead use standard template to line things up in a stackpanel.
A headeredcontentcontrol has a template with a stackpanel to put a header above it's content. This xaml re-templates to use a grid with two columns. The size of the first is shared across the scope of it's containing stackpanel.
You can also avoid repeating all your standard styling and whatnot by applying it in the template.
My markup uses simplified example controls for clarity.
Your textboxes ( or whichever control you wanted to label ) complete with binding, tooltip etc go in the content of a headeredcontentcontrol. The label gets it's value from the header property.
The shared size scope and auto size on the column means all the instances end up with the maximum necessary width a label in that stackpanel requests. So the right column of controls (textboxes) lines up.
<StackPanel Grid.IsSharedSizeScope="True">
<StackPanel.Resources>
<Style TargetType="HeaderedContentControl">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="HeaderedContentControl">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="L" Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Content="{TemplateBinding Header}"
Margin="2,2,4,2"
/>
<ContentControl Content="{TemplateBinding Content}"
Grid.Column="1"
/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</StackPanel.Resources>
<HeaderedContentControl Header="Label 1:">
<TextBox Text="Some textbox"/>
</HeaderedContentControl>
<HeaderedContentControl Header="A much longer label:">
<TextBox Text="A second textbox"/>
</HeaderedContentControl>
</StackPanel>

Star sizing Grid columns in an ItemsControl?

I have a ListBox with an ItemsControl bound to a collection in my view model. I'm trying to use star sizing on a grid column within the DataTemplate and set the element within that column (a progress bar) to stretch. This normally would take up all available horizontal space in the grid however nested in an ItemsControl this does not seem to be the case. I've done a bit of reading around & there seems to be known issues using these controls together. Is there a solution to this?
<ListBox>
<ItemsControl ItemsSource="{Binding WebMappingSourcesCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid
ShowGridLines="True"
Grid.IsSharedSizeScope="true"
>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<CheckBox
Grid.Column="0"
HorizontalAlignment="Center" />
<TextBlock
Grid.Column="1"
Text="{Binding Name}"/>
<ProgressBar Grid.Column="2"
Minimum="0"
Maximum="100"
Value="30"
HorizontalContentAlignment="Stretch"
HorizontalAlignment="Stretch"
MaxHeight="15"
/>
<!-- etc. etc. -->
Try setting HorizontalContentAlignment on your listbox item by giving ItemContainerStyle like below.
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>

Textblock wrapping incorrectly in a grid with star width

I'm using a ListBox, the item template is a grid with two columns. each column needs to take up half of the available space.
If the text within one of these columns gets too big then I need it to wrap.
I'm using the following code:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" BorderBrush="HotPink" BorderThickness="2">
<TextBlock Text="{Binding Title}"
FontFamily="Arial"
FontSize="16"/>
</Border>
<Border Grid.Column="1" BorderBrush="HotPink" BorderThickness="2">
<TextBlock Text="{Binding Description}"
FontFamily="Arial"
FontSize="16"
TextWrapping="Wrap"/>
</Border>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
This works fine when the text isn't long. If the text is too long then the second column starts taking up as much space as possible and then overflows off the screen.
I've used the Hot Pink borders to show the outline of each column in the following image.
Is there anyway to get text wrapping to work in this way?
This is not possible. You will have to implement your own panel which ensures the available space is always evenly distributed between the controls using the Measure and Arrange override methods.

What stops the tab?

I have the following xaml:
<ListBox ItemsSource="{Binding Path=ItemProperties.GeneralProperties}" Grid.Row="1"
Margin="0" Style="{StaticResource ListBoxStyle1}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="180" />
<ColumnDefinition Width="320" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" Grid.Column="0" />
<ContentPresenter Content="{Binding Converter={StaticResource PropertyInput}}" Grid.Column="1" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
where the ContentPresenter contains a TextBox, or a ComboBox, or a CheckBox.
To switch between the controls I need twice press the tab. Why???
I've already tried to comment the whole first column, without the TextBlock, unsuccessfully.
This worked for me for a DataGrid (which has a similar templating system).
<UserControl.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
</Style>
</UserControl.Resources>
Then anything which is a TabStop within the datagrid would work as a tabstop, but nothing else. Sorry I'm not sure what the equivalent code is for ListBox - but you may be able to figure it out from this.

Lock a grid column in place when changing GridViewColumn width

I have a DataTemplate for a gridview column, it has 2 items in it, an image and a text block, I want to "lock" the image to the left side of the column, even if the user expand the width of the column, I want the image to stay put.
any ideas ?
Try to set HorizontalContentAlignment to Stretch for GridViewColumnHeader
<ListView ...>
<ListView.Resources>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.Resources>
<!--...-->
</ListView>
Then your HeaderTemplate can look something like this if you want the TextBlock to be centered. Otherwise just remove the HorizontalAlignment
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Grid.Column="0" .../>
<TextBlock Grid.Column="1" HorizontalAlignment="Center" .../>
</Grid>
</DataTemplate>

Resources