Dynamic DataTemplate Width
I want to design the my application to have dynamic width that is custom to the dimensions of the phone. The code below is the data template i'm using for the listbox i have:
<DataTemplate x:Key="FlightsTemplate">
<StackPanel Orientation="Horizontal" Margin="3" Background="#FF171717" Height="35">
<TextBlock FontSize="19" Margin="1,0,0,0" TextAlignment="Center" Width="60" Text="{Binding text1}"/>
<TextBlock FontSize="19" Margin="1,0,0,0" TextAlignment="Center" Width="80" Text="{Binding text2}"/>
<TextBlock FontSize="19" Margin="1,0,0,0" TextAlignment="Center" Width="150" Text="{Binding text3}"/>
<TextBlock FontSize="19" Margin="1,0,0,0" TextAlignment="Center" Width="90" Text="{Binding text4}"/>
<TextBlock FontSize="19" Margin="1,0,0,0" TextAlignment="Center" Width="70" Text="{Binding text5}"/>
开发者_开发知识库 </StackPanel>
</DataTemplate>
How can i set the width property to be relative to the phone resolution which i know can be retrieved using the following line of code:
System.Windows.Application.Current.Host.Content.ActualWidth
Thank you in advance,
You need to set the HorizontalContentAlignment
for the ListBox's ItemContainerStyle
Like this:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
精彩评论