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

  • Home
  • SEARCH
  • 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 9113225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:55:04+00:00 2026-06-17T03:55:04+00:00

I have a listbox in which I render data using multiple datatemplates, and I

  • 0

I have a listbox in which I render data using multiple datatemplates, and I use a datatemplate selector to route data to the appropriate template.

Each template has it’s own layout using a grid. The first column of every grid inside the template is a textblock and I want them aligned to left. The next item is another textblock which should be aligned towards the maximum width of the first textblock (something similar to a data entry form). I’m using Grid.IsSharedSizeScope for this, but not able to achieve this. Below is my code:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">      

<Page.Resources>

 <DataTemplate x:Key="DefaultTemplate">
            <Border BorderThickness="1" CornerRadius="3" BorderBrush="LightGray">
            <TextBlock Text="{Binding Name}"></TextBlock>
            </Border>
        </DataTemplate>
        <DataTemplate x:Key="ShortFieldTemplate">
            <Grid Grid.IsSharedSizeScope="True">

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>  
                <TextBlock Text="Age:"  Grid.Column="0"></TextBlock>
                <TextBlock Text="{Binding Age}" Grid.Column="1"></TextBlock>
            </Grid>            
        </DataTemplate>
        <DataTemplate x:Key="LongFieldTemplate">
            <Grid Grid.IsSharedSizeScope="True">

                    <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
                        <ColumnDefinition/>                        
                    </Grid.ColumnDefinitions>               
                <TextBlock Text="This should be the name:" Grid.Column="0"></TextBlock>
                <TextBlock Text="{Binding Name}" Grid.Column="1"></TextBlock>                
                </Grid>                      
        </DataTemplate>

        <GridSplitterTextTrim:MyFirstTemplateSelector x:Key="MyFirstTemplateSelector" 
                                                      DefaultDataTemplate="{StaticResource DefaultTemplate}"
                                                      ShortFieldDataTemplate="{StaticResource ShortFieldTemplate}"
                                                      LongFieldDataTemplate="{StaticResource LongFieldTemplate}"
                                                      />

</Page.Resources>

   <Grid x:Name="LayoutRoot" Grid.IsSharedSizeScope="True">

    <Grid Grid.Column="2" Background="Green" >

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <ListBox ItemsSource="{Binding Items}" Grid.Row="0" Grid.Column="0" x:Name="listbox" ItemTemplateSelector="{StaticResource MyFirstTemplateSelector}">  
            </ListBox>
        </Grid>   
   </Grid>  
</Page>   

..and my object model

 public class ShortField : INotifyPropertyChanged
    {
        private string _name;
        public string Age
        {
            get { return _name; }
            set
            {
                _name = value;
                InvokePropertyChanged(new PropertyChangedEventArgs("Age"));
            }
        }

        private string _currentValue;
        public string CurrentValue
        {
            get { return _currentValue; }
            set
            {
                _currentValue = value;
                InvokePropertyChanged(new PropertyChangedEventArgs("CurrentValue"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void InvokePropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, e);
        }

        public static List<ShortField> GetShortFields()
        {
            return new List<ShortField>()
                       {
                           new ShortField() {Age = "10"},
                           new ShortField() {Age = "21"},
                       };
        }
    }

How do I get this alignment right? I thought Grid.IsSharedScope should do the trick. Is that correct or is there any other way?

Thanks in advance,
-Mike

  • 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-06-17T03:55:05+00:00Added an answer on June 17, 2026 at 3:55 am

    try to set

    <ListBox Grid.IsSharedSizeScope="True"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListBox which uses a DataTemplate to render databound items. The XAML
I have a ListBox which happily displays data using a code-behind MVVM object. However,
I have a databound listbox which is actually displaying two columns of data. It
I have a Listbox DataTemplate which looks like this <DataTemplate> <StackPanel Margin=0,24,0,24> <StackPanel toolkit:TiltEffect.IsTiltEnabled=true>
hello everybody i have a listbox within which is a datatemplate.Inside it is checkbox,textbox,label...Wat
I have a ListBox which contains all the Categories and each category will contain
I have a listbox which has couple of items. When double clicked on each
I have a listbox which populated from using a datatable. I have a Add
I have a ListBox which is made up of Grid Items in Multiple SelectionMode
I have a listbox with a lot of items which are expensive to render.

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.