UserControl中的TextBox不会占用所有空间 [英] TextBox in UserControl doesn't take all the space

查看:79
本文介绍了UserControl中的TextBox不会占用所有空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含自定义项目的WPF列表框.每个项目都是一个用户控件,由一个带有两个文本框的网格组成.我希望正确的人占据所有空间以填充ListBoxItem.但是我要做的就是,项目本身占用整个空间,而文本框占用其内容的大小.

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.

这是列表框:

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

以及用户控件:

<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>

在一些MSDN帖子上以及在此处有关堆栈溢出的研究之后,我已经尝试了以下内容:将Listbox.ItemContainerStyle的Horizo​​ntalContentAlignment设置为Stretch.我使用了一些边界来查找出现问题的部分. ListBoxItems似乎也占据了整个宽度,用户控件及其网格.尽管我认为网格的ColumnDefinition内的Width="*"会做到这一点,但文本框并不会占用所有空间.
另一个想法是将文本框的宽度绑定到其父尺寸,但随后它也占用了左侧文本框的空间(这很有意义,因为它获得了整个宽度),并且减去该宽度似乎不起作用.

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.

我在做什么错了?

推荐答案

您必须为此更改UserControl代码:

You have to change your UserControl code from this:

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

对此:

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

这篇关于UserControl中的TextBox不会占用所有空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆