jeudi 1 mai 2014

c# - enfant textbox ne s'étirera pas lorsque grille dans ItemTemplate de la ListBox est divisée en deux colonnes - Stack Overflow


I want to have a Grid with two columns in a ListBox's ItemTemplate, splitting it in half. I also want to have two TextBox controls in the grid that stretch fully. I am currently using the following XAML:


<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>

<TextBox Grid.Column="0" TextWrapping="Wrap" AcceptsReturn="True" InputScope="Text" Text="{Binding Key, Mode=TwoWay}"></TextBox>
<TextBox Grid.Column="1" TextWrapping="Wrap" AcceptsReturn="True" InputScope="Text" Text="{Binding Value, Mode=TwoWay}"></TextBox>
</Grid>

Fortunately, it looks like the columns split the grid in half. Unfortunately, it looks like the two TextBox controls don't stretch to use all the available horizontal space. Here's an image:


How the previous XAML turns out


If I put enough text in the textboxes, they stretch to fill all available horizontal space evenly. The problem is that they're not stretching when the text isn't in the TextBox controls.


I've tried removing the column definitions and setting both of the TextBox controls' widths to * and .5*, but that results in some exception of type System.Exception to be thrown before the page renders. I've also tried setting the column definitions' MinWidth property to * and .5*, but that also causes the same type of exception to be thrown.


How can I properly make the columns take 50% of the available width and the controls inside it to stretch accordingly?




You can do one thing inside the code in the initialize part you can specify both column width as Application.Current.RootVisual.RenderSize.Width/2 and to both of them so your problem will be solved.




I found another soultion that works in XAML! Simply put this under your ListBox tag:


<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListBox.ItemContainerStyle>


I want to have a Grid with two columns in a ListBox's ItemTemplate, splitting it in half. I also want to have two TextBox controls in the grid that stretch fully. I am currently using the following XAML:


<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>

<TextBox Grid.Column="0" TextWrapping="Wrap" AcceptsReturn="True" InputScope="Text" Text="{Binding Key, Mode=TwoWay}"></TextBox>
<TextBox Grid.Column="1" TextWrapping="Wrap" AcceptsReturn="True" InputScope="Text" Text="{Binding Value, Mode=TwoWay}"></TextBox>
</Grid>

Fortunately, it looks like the columns split the grid in half. Unfortunately, it looks like the two TextBox controls don't stretch to use all the available horizontal space. Here's an image:


How the previous XAML turns out


If I put enough text in the textboxes, they stretch to fill all available horizontal space evenly. The problem is that they're not stretching when the text isn't in the TextBox controls.


I've tried removing the column definitions and setting both of the TextBox controls' widths to * and .5*, but that results in some exception of type System.Exception to be thrown before the page renders. I've also tried setting the column definitions' MinWidth property to * and .5*, but that also causes the same type of exception to be thrown.


How can I properly make the columns take 50% of the available width and the controls inside it to stretch accordingly?



You can do one thing inside the code in the initialize part you can specify both column width as Application.Current.RootVisual.RenderSize.Width/2 and to both of them so your problem will be solved.



I found another soultion that works in XAML! Simply put this under your ListBox tag:


<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListBox.ItemContainerStyle>

0 commentaires:

Enregistrer un commentaire