2017-10-10 56 views
1

之前在这里解释我的问题无限增长的是我的代码:文本框宽度与文本

<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="&#xf00c;" Grid.Column="2" Grid.Row="0" /> 
       <Button Style="{DynamicResource MetroCircleButtonStyle}" Grid.RowSpan="3" Foreground="Red" FontSize="13" Width="50" FontFamily="{StaticResource FontAwesome}" Content="&#xf00d;" 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> 

我的问题是,当我写在文本框中(在XAML中最后一个元素)的文本,该文本框的宽度增长用它。现在我知道有一个maxwidth属性,但由于我没有为我的网格列定义宽度,我无法将其绑定到我的文本框的宽度。我不想根据真实像素设置宽度,因为我希望我的应用程序“可调整大小”。我也试图创建一个像边框一样的假控件,并将文本框的宽度绑定到它,但它不起作用。我该如何解决这个问题?

回答

1

首先,我们需要让模板假定其父项的大小。

  1. 在文本框中设置TextWrapping =“WrapWithOverflow”。
  2. 至少一列的宽度必须为*,而不是默认的Auto。这将导致网格自行调整到其父项。第二栏对我来说似乎是最好的角色。
  3. 给网格HorizontalAlignment="Left",所以它不会最终居中。
  4. 你可以摆脱StackPanel;它没有为你做任何事情。

其次,我们需要在DataGrid中进行一些更改以约束其行细节区域的宽度。

这是我测试过的例子;你的ItemsSource当然会有所不同。

<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="&#xf00c;" Grid.Column="2" Grid.Row="0" /> 
       <Button Style="{DynamicResource MetroCircleButtonStyle}" Grid.RowSpan="3" Foreground="Red" FontSize="13" Width="50" Content="&#xf00d;" 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> 

我是怎么算出这个?

我加入预览鼠标按下处理程序到网格中的DataTemplate:

<Grid Margin="0,10" Background="WhiteSmoke" PreviewMouseDown="Grid_PreviewMouseDown"> 

然我的测试程序和一些测试数据,点击一排,扩大该行的详细信息,并在该行的详细信息点击区。这是处理我为鼠标事件:

private void Grid_PreviewMouseDown(object sender, MouseButtonEventArgs e) 
{ 
    List<DependencyObject> parents = new List<DependencyObject>(); 
    var parent = VisualTreeHelper.GetParent(sender as DependencyObject); 
    while (parent != null) 
    { 
     parents.Add(parent); 
     parent = VisualTreeHelper.GetParent(parent); 
    } 

    ; 
} 

我设置一个断点,并在监视窗口看着parents

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

网格的母公司为DataGridDetailsPresenter

+  [0] {System.Windows.Controls.Primitives.DataGridDetailsPresenter} System.Windows.DependencyObject {System.Windows.Controls.Primitives.DataGridDetailsPresenter} 

他的父母是SelectiveScrollingGrid,并且环比上涨。通过简单的试验和错误,我找到了我想要的ActualWidth的父母,并且受此限制。

我发现了另一种将所需宽度作为DataGrid本身的特性而不是数据模板应用的方式。这使您可以使用任意的细节模板,而无需单独修改每个模板以使用正确的宽度。

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

当文本框宽度超过尺寸 –

+0

@DanieleSartori对不起,我犯了一个错误。至少一列需要宽度为'*'。 –

+0

问题仍然存在 –