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 7194797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:26:47+00:00 2026-05-28T20:26:47+00:00

I have a WPF listbox with custom items. Each item is a user control

  • 0

I have a WPF listbox with custom items. Each item is a user control consisting of a grid with two textboxes. I want that the right one takes all the space to fill the ListBoxItem. But all I get working is that the item itself takes the whole space but the textbox takes the size of its content.

So this is the listbox:

    <ListBox x:Name="leftListBox" Grid.Column="0" Margin="10" 
             HorizontalContentAlignment="Stretch">
        <ListBox.ItemTemplate>
            <DataTemplate>                    
                    <local:CustomLine />                    
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

And the user control:

<UserControl x:Class="SharpComparer.CustomLine"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="30">
    <UserControl.Resources>
        <Style TargetType="{x:Type TextBox}">
             <Setter Property="VerticalAlignment" Value="Center" />
        </Style>
    </UserControl.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="40" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <TextBox x:Name="NumberColumn" x:FieldModifier="public" 
                 Text="{Binding LineNumber}"
                 Grid.Column="0" HorizontalAlignment="Right" />
        <TextBox x:Name="TextColumn" x:FieldModifier="public" 
                 Text="{Binding Text}"
                 Grid.Column="1" HorizontalAlignment="Left" />
    </Grid>
</UserControl>

What I have already tried after some research at some MSDN posts and around here on Stack Overflow: Setting the HorizontalContentAlignment to Stretch for Listbox.ItemContainerStyle. I used some borders to find the piece which makes problems. The ListBoxItems seem to take the whole width, the usercontrol and its grid, too. The textbox does not take all the space although I thought that Width="*" inside the grid’s ColumnDefinition would do that.
Another idea was to bind the textbox width to its parent size, but then it also takes the space of the left textbox (which makes sense, because it gets the whole width) and subtracting this width doesn’t seem to work.

What am I doing wrong?

  • 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-28T20:26:49+00:00Added an answer on May 28, 2026 at 8:26 pm

    You have to change your UserControl code from this:

    <TextBox x:Name="TextColumn" x:FieldModifier="public" 
                     Text="{Binding Text}"
                     Grid.Column="1" HorizontalAlignment="Left" />
    

    to this:

    <TextBox x:Name="TextColumn" x:FieldModifier="public" 
                     Text="{Binding Text}"
                     Grid.Column="1" HorizontalAlignment="Stretch" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF application with ListBox that display list of items. Each item
Each item in a WPF ListBox control seems to have a bit of left
I have a WPF TabControl with two TabItems. Each TabItem contains a ListBox with
I have a WPF listbox control that is declaratively bound to a textbox. The
I have a WPF Listbox control and I would like to allow the user
I have a ListBox that contains a number of User items that are DataTemplate
I have a WPF ListBox that typically shows 4 or 5 items. For my
I have a WPF ListBox that I would like to Enable multiple selection in
I have an instance of ObservableCollection bound to a WPF listbox with two separate
I have created styled a ListBox in WPF so that it is rendered as

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.