C# / WPF - DataGrid - 将 RowDetails 中文本框的宽度绑定到包含 DataGrid 的宽度
我的问题与此类似;
除了我希望行详细信息永远不会超过列的宽度它跨越。
|--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 的宽度,这有那么多要求吗? :)
My problem is similar to this one;
Except I would like row details never to exceed the width of the columns it spans.
|--0--|--1--|--2--|--3--|--4--|
|---------Row-Details---------|
I have tried AreRowDetailsFrozen
and this had no effect. I have also tried binding to the parent Grid's actual width (OneWay) but this causes the width to exceed that of my two screens.
Here is my current attempt (simplified);
<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>
This results in the following;
|--0--|--1--|--2--|--3--|--4--|
|---------Row-Details are as wide as the longest row in their content ---------|
Binding the width of the TextBox to any parent container (Grid, DataGrid, ItemsPresenter):
Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}, Path=ActualWidth, Mode=OneWay}"
Results in:
|------Viewable Area-------|
|---- Columns ----|
|---------Row-Details --------------------------------------------------------------|
It's very frustrating, I just want the Row Details not to change the width of the DataGrid, is that so much to ask? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实现此目的的唯一方法是更改 DataGridRow ControlTemplate。在那里,我们可以将行详细信息主机宽度(DataGridDetailsPresenter)绑定到单元格的宽度。例如:
希望这有帮助。
The only way to accomplish this is to change DataGridRow ControlTemplate. There we can bind row details host width (DataGridDetailsPresenter) to width of cells. For example:
Hope this helps.
我在这里回答了类似的问题 DataGrid RowDetails 宽度问题
I answered a similar question here DataGrid RowDetails Width problem
我找到了另一种解决问题的方法:
这可以防止您使用常量值(例如“6”) - 如果有人设置了
DataGrid.RowHeaderWidth
,则该常量值不适合 - 和转换器。我已将其添加到
DataGrid.LoadingRowDetails
事件处理程序中,因为我已经以其他方式调整了 RowDetails。I've found another way to fix the Problem:
This prevents you from using constant values (e.g. '6') - which won't fit, if someone set
DataGrid.RowHeaderWidth
- and Converters.I've added this to the
DataGrid.LoadingRowDetails
event handler, since I am already tweaking the RowDetails in some other ways.