C# 文本框宽度随文本无限增长
在解释我的问题之前,这里是我的代码:C# 文本框宽度随文本无限增长,c#,wpf,xaml,C#,Wpf,Xaml,在解释我的问题之前,这里是我的代码: <DataGrid.RowDetailsTemplate> <DataTemplate> <StackPanel Background="WhiteSmoke" > <Grid Margin="0,10"> <Grid.ColumnDefinitions> <ColumnDe
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Background="WhiteSmoke" >
<Grid Margin="0,10">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="10" />
<RowDefinition Height="Auto" />
<RowDefinition Height="10" />
<RowDefinition Height="Auto" />
<RowDefinition Height="10" />
<RowDefinition Height="Auto" />
<RowDefinition Height="10" />
<RowDefinition Height="Auto" />
<RowDefinition Height="10" />
</Grid.RowDefinitions>
<TextBlock Text="Name: " FontWeight="Bold" Grid.Row="0" />
<TextBlock x:Name="parametro" Text="{Binding Username}" Grid.Column="1" Grid.Row="0" />
<TextBlock Text="Creation Date: " FontWeight="Bold" Grid.Row="2" />
<TextBlock Text="{Binding CreationDate}" Grid.Column="1" Grid.Row="2" />
<TextBlock Text="Creation User: " FontWeight="Bold" Grid.Row="4" />
<TextBlock Text="{Binding CreationUser}" Grid.Column="1" Grid.Row="4" />
<Button Style="{DynamicResource MetroCircleButtonStyle}" Grid.RowSpan="3" Foreground="Green" FontSize="13" Width="50" FontFamily="{StaticResource FontAwesome}" Content="" Grid.Column="2" Grid.Row="0" />
<Button Style="{DynamicResource MetroCircleButtonStyle}" Grid.RowSpan="3" Foreground="Red" FontSize="13" Width="50" FontFamily="{StaticResource FontAwesome}" Content="" Grid.Column="3" Grid.Row="0"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.DeleteUser}"
CommandParameter="{Binding ElementName=parametro, Path=Text}"/>
<ComboBox x:Name="combo" SelectedItem="{Binding Role}" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}},
Path=DataContext.Roles}" Grid.Row="6" Grid.Column="1"/>
<TextBlock Text="Ruolo: " FontWeight="Bold" Grid.Row="6" Grid.Column="0"/>
<TextBlock Text="Descrizione: " FontWeight="Bold" Grid.Row="8" Grid.Column="0"/>
<TextBox Grid.Row="8" Grid.Column="1"></TextBox>
</Grid>
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
我的问题是,当我在textbox(xaml中的最后一个元素)中写入文本时,textbox的宽度会随之增加。现在我知道有一个maxwidth属性,但由于我没有为网格列定义宽度,所以无法将其绑定到文本框的宽度。我不想根据实际像素设置宽度,因为我希望我的应用程序可以“调整大小”。我还试图创建一个类似边框的假控件,并将文本框的宽度绑定到它,但它不起作用。如何解决这个问题?首先,我们需要使模板假定为其父模板的大小
*
,而不是默认的Auto
。这将导致栅格根据其父栅格调整自身大小。在我看来,第二栏最适合这个角色李>
HorizontalAlignment=“Left”
,使其不会最终居中李>
<DataGrid
ItemsSource="{Binding Items}"
AutoGenerateColumns="False"
RowHeaderWidth="0"
>
<!--
ScrollContentPresenter includes the row header column.
DataGridDetailsPresenter doesn't. So we set RowHeaderWidth="0"
above to avoid making the details too wide by an amount equal to
the width of the row header.
This is just cosmetic, so leave RowHeaderWidth alone if you have
a requirement to keep the row header visible.
-->
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Grid
Margin="0,10"
Background="WhiteSmoke"
HorizontalAlignment="Left"
Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="10" />
<RowDefinition Height="Auto" />
<RowDefinition Height="10" />
<RowDefinition Height="Auto" />
<RowDefinition Height="10" />
<RowDefinition Height="Auto" />
<RowDefinition Height="10" />
<RowDefinition Height="Auto" />
<RowDefinition Height="10" />
</Grid.RowDefinitions>
<TextBlock Text="Name: " FontWeight="Bold" Grid.Row="0" />
<TextBlock x:Name="parametro" Text="{Binding Username}" Grid.Column="1" Grid.Row="0" />
<TextBlock Text="Creation Date: " FontWeight="Bold" Grid.Row="2" />
<TextBlock Text="{Binding CreationDate}" Grid.Column="1" Grid.Row="2" />
<TextBlock Text="Creation User: " FontWeight="Bold" Grid.Row="4" />
<TextBlock Text="{Binding CreationUser}" Grid.Column="1" Grid.Row="4" />
<Button Style="{DynamicResource MetroCircleButtonStyle}" Grid.RowSpan="3" Foreground="Green" FontSize="13" Width="50" Content="" Grid.Column="2" Grid.Row="0" />
<Button Style="{DynamicResource MetroCircleButtonStyle}" Grid.RowSpan="3" Foreground="Red" FontSize="13" Width="50" Content="" Grid.Column="3" Grid.Row="0"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.DeleteUser}"
CommandParameter="{Binding ElementName=parametro, Path=Text}"/>
<ComboBox MaxWidth="185" x:Name="combo" SelectedItem="{Binding Role}" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}},
Path=DataContext.Roles}" Grid.Row="6" Grid.Column="1"/>
<TextBlock Text="Ruolo: " FontWeight="Bold" Grid.Row="6" Grid.Column="0"/>
<TextBlock Text="Descrizione: " FontWeight="Bold" Grid.Row="8" Grid.Column="0"/>
<TextBox
Grid.Row="8"
TextWrapping="WrapWithOverflow"
Grid.Column="1"
></TextBox>
</Grid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
网格的父级是DataGridDetailsPresenter:
+ [0] {System.Windows.Controls.Primitives.DataGridDetailsPresenter} System.Windows.DependencyObject {System.Windows.Controls.Primitives.DataGridDetailsPresenter}
他的父母是选择性滚动网格
,处于链的上游。通过简单的尝试和错误,我找到了具有我想要的ActualWidth
的父级,并绑定到该父级
我找到了另一种将所需宽度作为DataGrid本身而不是datatemplate的特性应用的方法。这使您可以使用任意详细信息模板,而无需单独修复每个模板以使用正确的宽度
<DataGrid.RowStyle>
<!--
This style exists only so we can use its Resources to declare
the DataGridDetailsPresenter style someplace where it'll be taken
as an implicit style for DataGridDetailsPresenter in this grid's
row details children.
-->
<Style TargetType="DataGridRow">
<Style.Resources>
<Style TargetType="DataGridDetailsPresenter">
<Setter
Property="Width"
Value="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
/>
</Style>
</Style.Resources>
</Style>
</DataGrid.RowStyle>
当文本框的宽度超过dimension@DanieleSartori对不起,我弄错了。要求至少有一列的宽度为*
。问题仍然存在remain@DanieleSartori我测试了我给你的代码。它应该可以工作——唯一的可能性是您使用它的上下文允许网格控件任意增长。我想看看设置相对网格列的大小。我现在下班了。明天我会用更多的代码更新我的帖子。但是,拥有此模板的datagrid不受影响
- parents Count = 16 System.Collections.Generic.List<System.Windows.DependencyObject>
+ [0] {System.Windows.Controls.Primitives.DataGridDetailsPresenter} System.Windows.DependencyObject {System.Windows.Controls.Primitives.DataGridDetailsPresenter}
+ [1] {System.Windows.Controls.Primitives.SelectiveScrollingGrid} System.Windows.DependencyObject {System.Windows.Controls.Primitives.SelectiveScrollingGrid}
+ [2] {System.Windows.Controls.Border} System.Windows.DependencyObject {System.Windows.Controls.Border}
+ [3] {System.Windows.Controls.DataGridRow} System.Windows.DependencyObject {System.Windows.Controls.DataGridRow}
+ [4] {System.Windows.Controls.Primitives.DataGridRowsPresenter} System.Windows.DependencyObject {System.Windows.Controls.Primitives.DataGridRowsPresenter}
+ [5] {System.Windows.Controls.ItemsPresenter} System.Windows.DependencyObject {System.Windows.Controls.ItemsPresenter}
+ [6] {System.Windows.Controls.ScrollContentPresenter} System.Windows.DependencyObject {System.Windows.Controls.ScrollContentPresenter}
+ [7] {System.Windows.Controls.Grid} System.Windows.DependencyObject {System.Windows.Controls.Grid}
+ [8] {System.Windows.Controls.ScrollViewer} System.Windows.DependencyObject {System.Windows.Controls.ScrollViewer}
+ [9] {System.Windows.Controls.Border} System.Windows.DependencyObject {System.Windows.Controls.Border}
+ [10] {System.Windows.Controls.DataGrid Items.Count:10} System.Windows.DependencyObject {System.Windows.Controls.DataGrid}
+ [11] {System.Windows.Controls.Grid} System.Windows.DependencyObject {System.Windows.Controls.Grid}
+ [12] {System.Windows.Controls.ContentPresenter} System.Windows.DependencyObject {System.Windows.Controls.ContentPresenter}
+ [13] {System.Windows.Documents.AdornerDecorator} System.Windows.DependencyObject {System.Windows.Documents.AdornerDecorator}
+ [14] {System.Windows.Controls.Border} System.Windows.DependencyObject {System.Windows.Controls.Border}
+ [15] {CS7Test02.MainWindow} System.Windows.DependencyObject {CS7Test02.MainWindow}
+ [0] {System.Windows.Controls.Primitives.DataGridDetailsPresenter} System.Windows.DependencyObject {System.Windows.Controls.Primitives.DataGridDetailsPresenter}
<DataGrid.RowStyle>
<!--
This style exists only so we can use its Resources to declare
the DataGridDetailsPresenter style someplace where it'll be taken
as an implicit style for DataGridDetailsPresenter in this grid's
row details children.
-->
<Style TargetType="DataGridRow">
<Style.Resources>
<Style TargetType="DataGridDetailsPresenter">
<Setter
Property="Width"
Value="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
/>
</Style>
</Style.Resources>
</Style>
</DataGrid.RowStyle>