I have the following code currently:
<DataTemplate DataType="{x:Type vm:SectionViewModel}">
<ScrollViewer>
<ItemsControl ItemsSource="{Binding ViewModels}">
</ItemsControl>
</ScrollViewer>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:StringViewModel}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Name="Left" Grid.Row="0" Grid.Column="0" Content="{Binding Label}"/>
<TextBox Name="Right" HorizontalAlignment="Stretch" Grid.Row="0" Grid.Column="1" Text="{Binding Value}"/>
</Grid>
</DataTemplate>
The ViewModels property bound to SectionViewModel ItemsControl is a list of StringViewModel. I want to insert each StringViewModel into some sort of content control in the ItemsControl. Currently I just have each StringViewModel to make its own Grid, but that leaves things unaligned. I’d like to insert these items into some sort of content control in ItemsControl, it doesn’t necessarily have to be a grid, but it should be within the ItemsControl. How can I do this? I’m also following MVVM, using MVVM Light.
EDIT: I modified the XAML to reflect how I currently have it setup.
If you want to control the width in the containing template you can use an inherited attached property:
It would be used thusly:
The use of a DockPanel will cause the TextBox width to automatically fill the remaining space.
On the other hand, if you want the two columns to be the same percentage size relative to each other, you could use star sizing on the columns:
A very simple solution is to hard-code the width inside the DockPanel instead of using an attached property:
Lastly, if you need the width to adjust based on label size you can use a grid with shared sizing:
WPF is just full of possibilities!