简体   繁体   English

WPF将文本框的宽度绑定到列表框中的网格列

[英]WPF Binding TextBox width to grid column within a list box

I'm trying to build a simple (in terms of design) screen which will allow users to enter multiple rows of data. 我正在尝试构建一个简单的(就设计而言)屏幕,该屏幕将允许用户输入多行数据。

The format of the screen is 4 columns ( TextBox , TextBox , ComboBox , TextBox ), with new rows added dynamically. 屏幕的格式为4列( TextBoxTextBoxComboBoxTextBox ),并动态添加新行。

I have tried 2 approaches initially - one using a DataGrid , and one using a ListView . 我最初尝试了2种方法-一种使用DataGrid ,一种使用ListView

The DataGrid caused many headaches with items retaining focus when they were clicked out of, and the ListView prevented me from being able to consistently access the underlying cell, instead returning the bound data object represented by the row. 当单击项目时, DataGrid引起很多麻烦,因为它们使焦点保持焦点,而ListView阻止了我始终如一地访问底层单元格,而是返回了该行表示的绑定数据对象。

My current approach uses a custom component which represents a row, and contains the 3 TextBox objects, and one ComboBox object. 我当前的方法使用一个代表行的自定义组件,其中包含3个TextBox对象和一个ComboBox对象。

One or more of these objects are displayed in a ListBox . 这些对象中的一个或多个显示在ListBox

This approach allows for handling events more consistently, but is visually less straightforward to get working. 这种方法可以更一致地处理事件,但是从视觉上讲,工作起来不太直接。

Currently, when a row is displayed, the first 3 columns in the grid (which have an explicitly defined width ) display fine, but the textbox in the final column does not expand to fill the available width. 当前,当显示一行时, grid的前3列(具有明确定义的width )显示良好,但是最后一列中的textbox不会展开以填充可用宽度。

The relevant code for the panel: 面板的相关代码:

 <DockPanel LastChildFill="True" Name="basePanel">
    <Grid x:Name="baseGrid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="60" />
            <ColumnDefinition Width="60"></ColumnDefinition>
            <ColumnDefinition Width="55"></ColumnDefinition>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <TextBox Name="txtYear" Grid.Column="0" Style="{StaticResource txtComponentStyle}" Text="{Binding Path=RatingYear, ValidatesOnDataErrors=True,  UpdateSourceTrigger=PropertyChanged}" />
        <TextBox Name="txtArrears" Grid.Column="1" Style="{StaticResource txtComponentStyle}" Text="{Binding Path=ArrearsAmount, ValidatesOnDataErrors=True,  UpdateSourceTrigger=PropertyChanged}" />
        <ComboBox Name="cmbChangeCode" Grid.Column="2" ItemsSource="{Binding Path=ChangeCodes, Mode=OneTime}" SelectedItem="{Binding Path=ChangeCode}" />
        <TextBox Name="txtComments" Grid.Column="3" Style="{StaticResource txtComponentStyle}" Text="{Binding Path=Comments, ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Stretch" />
    </Grid>
</DockPanel>

When I select a row in the ListBox , I can see that the object itself fills the entire width, yet the text box in the final column only expands to fit the content. 当我在ListBox选择一行时,我可以看到对象本身填满了整个宽度,但是最后一列中的文本框仅扩展为适合内容。

Setting the width in the column definition to * should (as I understand it) cause the column to expand to fille the available space, and setting the HorizontalAlignment property on the textbox to Stretch should cause the text box to fill the column. 将列定义中的宽度设置为*应该(据我所知)会使列扩展以填充可用空间,而将textbox上的HorizontalAlignment属性设置为Stretch则应导致文本框填充列。

The code below creates the ListBox . 下面的代码创建ListBox

 <UserControl.Resources>
    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Border BorderBrush="Black" BorderThickness="0.5" Margin="1">
                        <DockPanel LastChildFill="True" HorizontalAlignment="Stretch" Background="DarkSlateBlue">
                            <Controls:ArrearsListEntry />
                        </DockPanel>
                    </Border>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>
<DockPanel LastChildFill="True" Background="Red">
    <ListBox IsSynchronizedWithCurrentItem="True" Name="lstRowData" ItemsSource="{Binding Path=Rows}" />
</DockPanel>

Is there a way to bind the width of the text box to the actual width of the column, or a property which will cause the text box to automatically expand to the available width? 是否可以将文本框的宽度绑定到列的实际宽度,还是可以使文本框自动扩展到可用宽度的属性?

The ListBoxItem's Content is left aligned by default. 默认情况下,ListBoxItem的内容保持对齐。 Add a Setter for the HorizontalContentAlignment to your ListBoxItem Style: 向您的ListBoxItem样式中添加用于HorizontalContentAlignment设置器:

<Style TargetType="ListBoxItem">
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="ContentTemplate">
        ...
    </Setter>
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM