Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Different sized items in an ItemsControl retaining required width only

$
0
0

Hi, I have a problem with what I thought would not be that complicated, but I have now spent a few hours stuck with what to try next.

I have a sample of the following at https://dl.dropboxusercontent.com/u/30760237/ItemsControlWidth.zip, which contains the following XAML..

<Window x:Class="ItemsControlWidth.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"><Grid Width="200"><!--<ScrollViewer Margin="20">--><ItemsControl ItemsSource="{Binding Data}" HorizontalContentAlignment="Left" Width="Auto"><ItemsControl.ItemTemplate><DataTemplate><!--<Border  Margin="5" Background="LightGreen"  CornerRadius="5">--><Grid Width="Auto" Background="Brown"><Grid.RowDefinitions><RowDefinition /><RowDefinition/></Grid.RowDefinitions><TextBlock Background="Pink"  Width="Auto" Text="{Binding Key}"/><TextBlock Width="Auto"  Background="Pink"  Grid.Row="1" Text="{Binding Value}"/></Grid><!--</Border>--></DataTemplate></ItemsControl.ItemTemplate></ItemsControl><!--</ScrollViewer>--></Grid></Window>

Basically, I have an ItemsControl, using an items template of a grid with 2 TextBlocks, where each item may have different sized text. What I am after is that each item is NOT stretched to the width of the Items control. However, as can be seen in the attached example, they are always stretched. If it worked as I wanted, I would see different sized items in the items control

I would have thought that setting the HorizontalContentAlignment on the items control to something other than stretch on the would stop this, but unfortunatley is doesn't. I removed my ScollViewer, and surrounding border (and colored the elements to see them), but they still all stretch.

Does anyone know a way around this? Can I have each item just wide enough to fix it's contents (ie the text and any margins etc)

Thanks in advance for any help.

Regards, Peter


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>