【问题标题】:C# / WPF - DataGrid - Binding the Width of a TextBox in RowDetails to that of the containing DataGridC# / WPF - DataGrid - 将 RowDetails 中的 TextBox 的宽度绑定到包含 DataGrid 的宽度
【发布时间】:2011-10-17 11:32:14
【问题描述】:

我的问题和这个类似;

除非我希望行详细信息永远不会超过它所跨越的列的宽度。

|--0--|--1--|--2--|--3--|--4--|
|---------Row-Details---------|

我试过AreRowDetailsFrozen,但没有效果。我也尝试绑定到父网格的实际宽度(OneWay),但这会导致宽度超过我的两个屏幕的宽度。

这是我目前的尝试(简化);

  <Grid>
    <DataGrid x:Name="Grid" 
              Grid.Row="1" 
              ItemsSource="{Binding Collection}"
              IsReadOnly="True"
              AutoGenerateColumns="False" 
              ColumnWidth="Auto"
              CanUserResizeColumns="False"
              CanUserResizeRows="False"
              RowDetailsVisibilityMode="VisibleWhenSelected"
              AreRowDetailsFrozen="True"
              SelectionUnit="FullRow"
              VerticalAlignment="Top"
              HorizontalAlignment="Center">
        <DataGrid.RowDetailsTemplate>
           <!-- Begin row details section. -->
           <DataTemplate>
               <TextBox DataContext="{Binding ErrorMessage}" 
                       IsReadOnly="True"
                       Margin="5"
                       BorderBrush="Transparent"
                       ScrollViewer.VerticalScrollBarVisibility="Auto"
                       ScrollViewer.CanContentScroll="True"
                       TextWrapping="Wrap"
                       Text="{Binding .}">
               </TextBox>
           </DataTemplate>
        </DataGrid.RowDetailsTemplate>
   </DataGrid>
  </Grid>

这会导致以下结果;

|--0--|--1--|--2--|--3--|--4--|
|---------Row-Details are as wide as the longest row in their content ---------|

将 TextBox 的宽度绑定到任何父容器(Grid、DataGrid、ItemsPresenter):

Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}, Path=ActualWidth, Mode=OneWay}"

结果:

                              |------Viewable Area-------|
|---- Columns ----|
|---------Row-Details --------------------------------------------------------------|

很郁闷,我只是想让Row Details不要改变DataGrid的宽度,有这么多要求吗? :)

【问题讨论】:

    标签: c# wpf width rowdetailstemplate


    【解决方案1】:

    完成此操作的唯一方法是更改​​ DataGridRow ControlTemplate。在那里,我们可以将行详细信息主机宽度 (DataGridDetailsPresenter) 绑定到单元格的宽度。例如:

    <Style x:Key="{x:Type dg:DataGridRow}" TargetType="{x:Type dg:DataGridRow}">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
        <Setter Property="ValidationErrorTemplate">
          <Setter.Value>
            <ControlTemplate>
              <TextBlock Margin="2,0,0,0" VerticalAlignment="Center" Foreground="Red" Text="!" />
            </ControlTemplate>
          </Setter.Value>
        </Setter>
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type dg:DataGridRow}">
              <Border x:Name="DGR_Border"
                      Background="{TemplateBinding Background}"
                      BorderBrush="{TemplateBinding BorderBrush}"
                      BorderThickness="{TemplateBinding BorderThickness}"
                      SnapsToDevicePixels="True">
                <dgp:SelectiveScrollingGrid>
                  <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                  </Grid.ColumnDefinitions>
    
                  <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto"/>
                  </Grid.RowDefinitions>
    
                  <dgp:DataGridCellsPresenter x:Name="cellPresenter" Grid.Column="1"
                                             ItemsPanel="{TemplateBinding ItemsPanel}"
                                             SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
    
                  <dgp:DataGridDetailsPresenter  dgp:SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding RelativeSource={RelativeSource AncestorType={x:Type dg:DataGrid}}, Path=AreRowDetailsFrozen, Converter={x:Static dg:DataGrid.RowDetailsScrollingConverter}, ConverterParameter={x:Static dg:SelectiveScrollingOrientation.Vertical}}"
                                                Grid.Column="1" Grid.Row="1"
                                                Visibility="{TemplateBinding DetailsVisibility}" Width="{Binding ElementName=cellsPresenter, Path=ActualWidth}"/>
    
                  <dgp:DataGridRowHeader dgp:SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical"  Grid.RowSpan="2"
                                        Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type dg:DataGrid}}, Path=HeadersVisibility, Converter={x:Static dg:DataGrid.HeadersVisibilityConverter}, ConverterParameter={x:Static dg:DataGridHeadersVisibility.Row}}"/>
                </dgp:SelectiveScrollingGrid>
              </Border>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    

    希望这会有所帮助。

    【讨论】:

    • 谢谢你,我会等一会儿看看是否有更简洁的方法,但这是我迄今为止看到的唯一可行的选择。非常感谢。
    【解决方案2】:

    我在这里回答了类似的问题DataGrid RowDetails Width problem

    这里的答案感觉像是一种解决方法,所以我做了一些研究并做了 在 Telerik 论坛上找到解决方案,因为我们使用他们的 辐射网格视图。结果证明该解决方案也适用于 DataGrid。

    关键是设置ScrollViewer.Horizo​​ntalScrollBarVisibility 属性禁用,请参见下面的示例。

    <DataGrid ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <Border>
                <TextBlock Foreground="White" Text="{Binding RowDetails}"
                           TextWrapping="Wrap"/>
            </Border>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate> </DataGrid>
    

    【讨论】:

      【解决方案3】:

      我找到了解决问题的另一种方法:

      private void GridOnLoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
      {
          var dataGridColumnHeadersPresenter = FindVisualChild<DataGridColumnHeadersPresenter>((DataGrid)sender);
          e.DetailsElement.SetBinding(WidthProperty, new Binding("ActualWidth") { Source = dataGridColumnHeadersPresenter });
      }
      

      这会阻止您使用常量值(例如“6”) - 如果有人设置了 DataGrid.RowHeaderWidth - 和转换器,这将不适合。

      我已将此添加到 DataGrid.LoadingRowDetails 事件处理程序中,因为我已经在以其他方式调整 RowDetails。

      【讨论】:

        猜你喜欢
        • 2011-05-11
        • 2015-03-20
        • 2017-08-07
        • 2016-11-05
        • 1970-01-01
        • 2011-11-29
        • 1970-01-01
        • 2013-07-26
        • 2012-06-06
        相关资源
        最近更新 更多