Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 723943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:09:57+00:00 2026-05-14T06:09:57+00:00

I have the following code currently: <DataTemplate DataType={x:Type vm:SectionViewModel}> <ScrollViewer> <ItemsControl ItemsSource={Binding ViewModels}> </ItemsControl>

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:09:58+00:00Added an answer on May 14, 2026 at 6:09 am

    If you want to control the width in the containing template you can use an inherited attached property:

    public class WidthInformation
    {
       // Use propa snippet to create LabelWidth property with this metadata:
       ... RegisterAttached("LabelWidth", typeof(double), typeof(WidthInformation), new FrameworkPropertyMetadata
       {
         Inherits = true
       });
    }
    

    It would be used thusly:

    <DataTemplate DataType="{x:Type vm:SectionViewModel}">      
      <ScrollViewer>      
        <ItemsControl ItemsSource="{Binding ViewModels}"
                      local:WidthInformation.LabelWidth="60" />
      </ScrollViewer>      
    </DataTemplate>      
    <DataTemplate DataType="{x:Type vm:StringViewModel}">      
      <DockPanel>
        <Label Content="{Binding Label}"
               Width="{Binding Path=(local:WidthInformation.LabelWidth)"/>      
        <TextBox Text="{Binding Value}"/>      
      </DockPanel>
    </DataTemplate>
    

    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:

    <DataTemplate DataType="{x:Type vm:StringViewModel}">      
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="2*" />
          <ColumnDefinition Width="3*" />
        </Grid.ColumnDefinitions>
        <Label Content="{Binding Label}" />      
        <TextBox Text="{Binding Value}" Grid.Column="1" />      
      </DockPanel>
    </DataTemplate>
    

    A very simple solution is to hard-code the width inside the DockPanel instead of using an attached property:

    <DataTemplate DataType="{x:Type vm:StringViewModel}">      
      <DockPanel>
        <Label Content="{Binding Label}" Width="80" />
        <TextBox Text="{Binding Value}"/>      
      </DockPanel>
    </DataTemplate>
    

    Lastly, if you need the width to adjust based on label size you can use a grid with shared sizing:

    <DataTemplate DataType="{x:Type vm:SectionViewModel}">      
      <ScrollViewer>      
        <ItemsControl ItemsSource="{Binding ViewModels}"
                      Grid.IsSharedSizeScope="true" />
      </ScrollViewer>      
    </DataTemplate>      
    <DataTemplate DataType="{x:Type vm:StringViewModel}">      
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto" SharedSizeGroup="Label" />
          <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Label Content="{Binding Label}" />      
        <TextBox Text="{Binding Value}" Grid.Column="1" />      
      </DockPanel>
    </DataTemplate>
    

    WPF is just full of possibilities!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 366k
  • Answers 366k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer 4 years, just 2 active days? Get it and make… May 14, 2026 at 4:37 pm
  • Editorial Team
    Editorial Team added an answer "Each is a base64-encoded 128-bit (presumably random) number, with the… May 14, 2026 at 4:37 pm
  • Editorial Team
    Editorial Team added an answer you can be sure if rails 3 installed with this:… May 14, 2026 at 4:36 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.