How to limit column height to another one's? - wpf

I have a grid in WPF which is made of 4 rows along 2 columns where column 1 holds an Image control and column 2 holds 4 Textblocks. Problem is, the Image control sizes itself to the Image size and extends the listbox's entry too much [Its in a DataTemplate] and makes everything look distorted. I dont want to manually set a max height/width because i want the Image to size itself to the size of the 4 textblocks that are alongside it. Any ideas?
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Image Source="{Binding Logo, Converter={StaticResource BSConverter}}" Grid.Row="0" Grid.RowSpan="4"
Grid.Column="0" Stretch="Uniform" SnapsToDevicePixels="True"/>
<TextBlock Text="{Binding Name}" Grid.Row="0" Grid.Column="1"/>
<TextBlock Text="{Binding Author}" Grid.Row="1" Grid.Column="1"/>
<TextBlock Text="{Binding Version}" Grid.Row="2" Grid.Column="1"/>
<TextBlock Text="{Binding Description}" Grid.Row="3" Grid.Column="1"/>
</Grid>
</DataTemplate>
Thanks in advance

You can use Grid.IsSharedSizeGroup on the Parent ListBox to make sure all of your items get the same Width for the first Column like this
<ListBox ...
Grid.IsSharedSizeScope="True">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="GroupA"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
...
For the Image height problem, you could bind Height to the ActualHeight of the Parent Grid with a FallbackValue of 1.0 (to ensure the Height of the Image doesn't effect the Height of the Grid)
<Image Source="{Binding Logo, Converter={StaticResource BSConverter}}"
Grid.Row="0"
Grid.RowSpan="4"
Grid.Column="0"
Stretch="Uniform" SnapsToDevicePixels="True"
Height="{Binding RelativeSource={RelativeSource AncestorType=Grid},
Path=ActualHeight,
FallbackValue=1.0}"/>

Modifying your containers slightly to make use of a StackPanel in conjunction with a Grid and referencing the StackPanel via ElementName binding should provide you the visuals you are looking for...
<DataTemplate>
<Grid HorizontalAlignment="Left" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,0,8,0" Source="{Binding Logo, Converter={StaticResource BSConverter}}"
Grid.Column="0" Stretch="Uniform" SnapsToDevicePixels="True"
Height="{Binding ElementName=Contents, Path=ActualHeight}"/>
<StackPanel VerticalAlignment="Top" Name="Contents" Grid.Column="1" Orientation="Vertical">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Author}"/>
<TextBlock Text="{Binding Version}"/>
<TextBlock Text="{Binding Description}"/>
</StackPanel>
</Grid>
</DataTemplate>

Related

Nested User Controls in top level Scroll Viewer not resulting in a scroll bar

I have a main window with a scroll viewer that houses and items control with a user control:
<ScrollViewer VerticalScrollBarVisibility="Visible" Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2">
<ItemsControl ItemsSource="{Binding oObsByDiv}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<uc:ucObservationsHeader/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
ucObservationsHeader shows a title bar with a nested list of observations in another user control. This runs off the page, and I have a scroll bar visible, but it does not allow any scrolling. I think the scroll viewer just sees the top level user control as not needing any scrolling, even though the nested content is scrolling way below the screen. How do I get this to work?
Here are the ucObservationsHeader and ucObservations xaml:
ObservationHeader:
<UserControl x:Class="ucObsevationsHeader"
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="300*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="100*"/>
</Grid.RowDefinitions>
<TextBox x:Name="divNum" Text="{Binding DivNum, Mode=OneWay}"
Grid.Column="0" Grid.Row="0" Margin="0,3,0,0"/>
<TextBox x:Name="divName" Text="{Binding DivName, Mode=OneWay}"
Grid.Column="1" Grid.Row="0" Margin="0,3,0,0"/>
<ItemsControl ItemsSource="{Binding lObs}" Grid.Row="1" Grid.Column="1">
<ItemsControl.ItemTemplate>
<DataTemplate>
<uc:ucObservationsView/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid></UserControl>
Observations:
<UserControl x:Class="ucObservationsView">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="25*"/>
<RowDefinition Height="25*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".2*"/>
<ColumnDefinition Width=".4*"/>
<ColumnDefinition Width=".4*"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="txtDivObs" HorizontalAlignment="Stretch" TextWrapping="Wrap" Text="{Binding obsFullNum, Mode=OneWay}" Grid.Column="0" Grid.Row="0"/>
<Label x:Name="lblOpenDate" HorizontalAlignment="Left" Content="Open " Grid.Column="1" Grid.Row="0"/>
<DatePicker x:Name="dtOpen" HorizontalAlignment="Right" SelectedDate="{Binding Opendate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.Row="0"/>
<Label x:Name="lblCLoseDate" HorizontalAlignment="Left" Content="Close " Grid.Column="2" Grid.Row="0"/>
<DatePicker x:Name="txtCloseDate" HorizontalAlignment="Right" SelectedDate="{Binding Closedate}" Grid.Column="2" Grid.Row="0"/>
<TextBox x:Name="txtDescription" HorizontalAlignment="Stretch" TextWrapping="Wrap" Text="{Binding Description}" VerticalAlignment="Top" Grid.ColumnSpan="3" Grid.Row="1"/>
<TextBox x:Name="txtImagePath" HorizontalAlignment="Stretch" TextWrapping="Wrap" Text="{Binding ImagePath}" Grid.Row="2" Grid.ColumnSpan="3">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding
DataContext.PreviewImage, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
</Grid>
</UserControl>
I'm doing this to show a layout with division headers for the Observations, and have structured my data accordingly. Without the scrollbar working this is useless though.
I suspect this may be related to your spelling mistake in ucObservationsHeader, although that should normally throw up a compile error:
<UserControl x:Class="ucObsevationsHeader"
Other than that I couldn't reproduce it, which suggests to me that it might be a theming issue. I think you're going to have to provide an mcve.
In my MainWindow I have 4 rows defined:
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="50"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
The 4th row is where my user control inside the scroll viewer resides. The Auto is what causes my problem, setting that to "200*" fixed the problem and the scroll bar appears as expected. I'm not sure why I set that to Auto in the first place but in case anyone has this similar situation this is something to look out for.

How can I achieve this view of Windows Store Download List? UWP - Xaml

I am trying to achieve the following view in downloads section of my app. But I cant seem to get it right.
I also tried to Google it but came up with nothing.
I know this is ListView and I can use a Grid but then I have to create multiple Columns in it and give them fixed width to get a good view but it doesn't seem right because it is supposed to be an adaptive view so how fixed width will help?
And if I provide * as width the Column goes to 0 and it doesn't work as I expect it to.
I also used RelativePanel mixed with StackPanel but that only worsens the view because when I resize the window the list doesn't get resized but stays at its original size.
Any help in this regard will be appreciated.
Update 13-Dec-2016
Downloads.xaml
<ListView Grid.Row="2"
x:Name="DownloadListView"
ItemsSource="{x:Bind DownloadList, Mode=OneWay}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}"
FontWeight="Bold"
TextTrimming="CharacterEllipsis"
Grid.ColumnSpan="2"/>
<TextBlock Text="{Binding Type}"
TextWrapping="Wrap"
Grid.Row="2"/>
<StackPanel Orientation="Horizontal"
Grid.Row="3"
Grid.ColumnSpan="2">
<TextBlock Text="MBs Received:"
Width="100"/>
<TextBlock Text="{Binding BytesReceived}"
HorizontalAlignment="Right"
/>
</StackPanel>
<StackPanel Orientation="Horizontal"
Grid.Row="4"
Grid.ColumnSpan="2">
<TextBlock Text="Total MBs:"
Width="100"/>
<TextBlock Text="{Binding TotalBytesToReceive, Mode=OneWay}"
HorizontalAlignment="Right"/>
</StackPanel>
<ProgressBar Value="{Binding Progress}"
Height="10"
Width="200"
HorizontalAlignment="Left"
Grid.Column="0"
Grid.ColumnSpan="3"
Grid.Row="5"/>
<Button Grid.Column="3"
Grid.Row="1"
Grid.RowSpan="2"
HorizontalAlignment="Center"
Background="Transparent"
Width="80"
Click="PauseResumeButton_Click"
Content="{Binding PauseResume, Mode=OneWay}"/>
<Button Grid.Column="3"
Grid.Row="3"
Grid.RowSpan="2"
Width="80"
HorizontalAlignment="Center"
Background="Transparent"
Click="CancelButton_Click"
Content="Cancel"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
This is only the ListView part of the page.
This doesn't work as the above image but it's somewhat close to it. This list view is only wide enough to put the items in it. It is not responsive as the UI changes but it's usable on mobile (I haven't checked on mobile) and pc. As it can be seen within the XAML design interface.

Inner grid present in listview item is not stretching

In my List view I use a grid to arrange the controls of list view item
here is the code
<ListView.ItemTemplate>
<DataTemplate>
<Border>
<!--<Label Width="Auto" HorizontalAlignment="Stretch" Height="3" Background="LightGray"></Label>-->
<Grid HorizontalAlignment="Stretch" MinWidth="220" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="65*"/>
<ColumnDefinition Width="10" />
<ColumnDefinition Width="25*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" FontWeight="Bold" Text="{Binding Path=Name}"/>
<TextBlock Grid.Row="0" Grid.Column="2" HorizontalAlignment="Right" Text="{Binding PlanDate.DateValue,StringFormat=d}"></TextBlock>
<TextBlock Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left" Text="{Binding Owner}"></TextBlock>
<TextBlock Grid.Row="1" Grid.Column="2" HorizontalAlignment="Right" Text="{Binding ForecastDate.DateValue,StringFormat=d}"></TextBlock>
</Grid>
<!--<Label Width="Auto" HorizontalAlignment="Stretch" Height="3" Background="LightGray"></Label>-->
</Border>
</DataTemplate>
</ListView.ItemTemplate>
Here the text blocks in grid coloumn 2 horizontal alignment is set to right .. still the texblocks are not been placed at the right . Am i doing some mistake here? Please advise
Try adding HorizontalContentAlignment="Stretch" to your ListView (the default value is Left and that's why you see it's not stretched).
You can remove HorizontalAlignment="Stretch" MinWidth="220" in your inner Grid.

How to do superscript in WP XAML?

Here is the usercontrol which is used in my WP8 app. It displays current time. The fontsize is specified by the screen which uses this control. I want AM to be on top but with inline with the text.
Here is my XAML code for control. Also note that Typography.Variants is not supported in WP8
<TextBlock Text="{Binding BindingHour}" FontWeight="Bold" Name="txtHour"
Grid.Row="0" Grid.Column="0"
FontSize="{Binding BindingHourFontSize}" />
<TextBlock Text=":" FontWeight="ExtraLight"
Grid.Row="0" Grid.Column="1"
FontSize="{Binding BindingColonFontSize}" />
<TextBlock Text="{Binding BindingMinute}" FontWeight="Thin"
Grid.Row="0" Grid.Column="2"
FontSize="{Binding BindingMinuteFontSize}"/>
<TextBlock Text="{Binding BindingAmPm}" FontWeight="SemiBold" Grid.Row="0" Grid.Column="3" />
<TextBlock Grid.Row="1" HorizontalAlignment="Center" FontWeight="SemiBold"
Text="{Binding BindingFreeText}" Grid.ColumnSpan="5"/>
Here is how it looks on the screen where i use above control.
Here is how i want it to look but not able to do it in XAML. Also, superscript and subscript is not supported in WP XAML. odd.
Well, it appears that you can do it the same way it is done via WPF, through the Typography.Variants attached property.
http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.windows.documents.typography.variants(v=vs.105).aspx
which allows you to specify superscript/subscript variants of the font. However, that's not really what you want. Your AM/PM appears vertically aligned within the grid control. If it appears too high relative to neighboring characters, simply push it down via its Margin.
Here is an example of the layout
<Grid
Width="auto"
Height="100"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
Grid.Row="0"
FontSize="80"
Text="09"/>
<TextBlock
Grid.Column="1"
Grid.Row="0"
VerticalAlignment="Center"
FontSize="50"
Text=":"/>
<TextBlock
Grid.Column="2"
Grid.Row="0"
FontSize="80"
Text="11"/>
<TextBlock
Margin="0 20 0 0"
Grid.Column="3"
Grid.Row="0"
FontSize="20"
Text="AM"/>
</Grid>
And a snapshot of how it looks
To keep relative positions the same while stretching/compressing to fill existing space, use ViewBoxes.
If you create the following, and paste the grid into each of the following ViewBoxes
<Grid
Width="auto"
Height="auto"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Viewbox Grid.ColumnSpan="2"></Viewbox>
<Viewbox Grid.Row="1"></Viewbox>
<Viewbox Grid.Row="1" Grid.Column="1"></Viewbox>
</Grid>
you get the following

WPF - Usercontrol height does not size properly

I have an issue I cannot figure out. I hope I can explain things enough.
Basically, I have a usercontrol that I'm looking to use as a sort of in window modal dialog.
<Grid>
<Rectangle Opacity=".75" Fill="White"/>
<Border Width="425" BorderBrush="LightGray" BorderThickness="2" CornerRadius="20,0,20,0" Padding="3">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="15"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<!-- First Name -->
<Label Grid.Row="0" Grid.Column="0" Content="First Name:"/>
<TextBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" Text="{Binding FirstName}" Margin="3"/>
<!-- Last Name -->
<Label Grid.Row="0" Grid.Column="3" Content="Last Name:"/>
<TextBox Grid.Row="0" Grid.Column="4" Grid.ColumnSpan="2" Text="{Binding LastName}" Margin="3"/>
<!-- Address -->
<Label Grid.Row="1" Grid.Column="0" Content="Address:"/>
<TextBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="5" Text="{Binding Address}" HorizontalAlignment="Stretch" Margin="3"/>
<!-- City -->
<Label Grid.Row="2" Grid.Column="0" Content="City:"/>
<TextBox Grid.Row="2" Grid.Column="1" Text="{Binding City}" Margin="3"/>
<!-- State -->
<Label Grid.Row="2" Grid.Column="2" Content="State:"/>
<ComboBox Grid.Row="2" Grid.Column="3" ItemsSource="{Binding States}" SelectedValue="{Binding State}" Margin="3"/>
<!-- Zip Code -->
<Label Grid.Row="2" Grid.Column="4" Content="Zip Code:"/>
<TextBox Grid.Row="2" Grid.Column="5" Text="{Binding ZipCode}" Margin="3"/>
<Button Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="6" Width="100" HorizontalAlignment="Center" Content="Save" Command="{Binding SaveCustomerCommand}"/>
</Grid>
</Border>
</Grid>
I also have a resourcedictionary containing a datatemplate to connect this usercontrol to it's viewmodel.
<DataTemplate DataType="{x:Type vm:CreateCustomerViewModel}">
<view:CreateCustomerView/>
</DataTemplate>
Finally, in the main window viewmodel, I create an instance of the control's viewmodel, and in the main window view, I am using an itemscontrol and binding it's itemssource property to the instance of the control's viewmodel.
<ItemsControl Height="600" Grid.Row="0" ItemsSource="{Binding CreateCustomerViewModel}" Grid.RowSpan="2" />
Now, my issue is using the itemscontrol in the main window, I've tried a few different ways, but I cannot get the control to be the height of the window. I'm not sure if I shouldn't be using an itemscontrol, or what I'm doing wrong. Any help is very much appreciated.
ItemsControl is for collections. By default it uses a StackPanel to contain its child elements, which disallows stretching in the stack direction (Vertical by default). For a single item use ContentControl (the base of things like Button and Label) instead:
<ContentControl Height="600" Grid.Row="0" Content="{Binding CreateCustomerViewModel}" Grid.RowSpan="2" />
You could try the following:
Put your ItemsControl in a Grid.
Declare your ItemsControl with VerticalContentAlignment="Stretch".
It shouldn't make any difference because it's the default setting, but declare your ItemsControl with VerticalAlignment="Stretch" and try removing the Height="600".

Resources