将DataGrid列宽绑定到另一个DataGrid的两个列 [英] Bind DataGrid Column Width to Two Colums of Another DataGrid
问题描述
问题:如何将 StatName.Width
绑定到 Samp1.ActualWidth + Samp2.ActualWidth
?
子-问题:
为什么我的 WidthConverter
函数从未执行过(断点从未命中)?
Why are my WidthConverter
functions never executed (breakpoints never hit)?
单个MultiBinding组件正确的< Binding ...
语法是什么?
What is the right <Binding ...
syntax for the individual MultiBinding components?
我尝试过的操作
我认为我需要分配 DataGridTextColumn.Width
和 MultiBinding
和某些类型的转换器。
I think I need to assign the DataGridTextColumn.Width
with MultiBinding
and some type of converter.
我尝试过在线收集各种示例的解决方案。我似乎无法将所有内容放在一起。
I have tried putting together a solution from various examples online. I can't seem to pull it all together.
编辑
使用<绑定ElementName = Samp1 Path = ActualWidth />
- 没有构建错误或警告。
- 未命中
Convert
函数 - 否输出中的相关消息
- No build errors or warnings.
- The
Convert
function is not hit - No relevant messages in Output
使用< Binding Source = {x:Reference Samp1} Path = ActualWidth />
- 生成错误(仍然允许执行):指定的强制转换无效。对于
< MultiBinding> ...< / MultiBinding>
。 -
转换
在加载时被调用了三次。调整
列Samp1
的大小时不会调用它。 - 每次执行
转换:
- Build error (that still allows execution): Specified cast is not valid. For
<MultiBinding>...</MultiBinding>
. Convert
is called three times when loading. It is not called when resizing columnSamp1
.- On each execution of
Convert:
System.Windows.Data错误:5:BindingExpression生成的值不是对目标财产有效。值= 40 MultiBindingExpression:目标元素是 DataGridTextColumn(HashCode = 64879470);目标属性为宽度(类型为 DataGridLength)
MultiBinding Attempt (xaml部分)
<Page.Resources>
<local:WidthConverter x:Key="WidthConverter" />
</Page.Resources>
<StackPanel>
<DataGrid IsReadOnly="True" HeadersVisibility="Column">
<DataGrid.Columns>
<DataGridTextColumn x:Name="Samp1" Binding="{Binding a}" Header="S1" />
<DataGridTextColumn x:Name="Samp2" Binding="{Binding b}" Header="S2" />
<DataGridTextColumn x:Name="Total" Binding="{Binding c}" Header="Tot" />
</DataGrid.Columns>
<local:MyGenericRecord a="5000" b="2500" c="7500" />
<local:MyGenericRecord a="1000" b="1500" c="2500" />
</DataGrid>
<DataGrid IsReadOnly="True" HeadersVisibility="Column">
<DataGrid.Columns>
<DataGridTextColumn x:Name="StatName" Binding="{Binding a}" Header="Stat">
<DataGridTextColumn.Width>
<MultiBinding Converter="{StaticResource WidthConverter}">
<Binding ElementName="Samp1" Path="ActualWidth" />
<Binding ElementName="Samp2" Path="ActualWidth" />
</MultiBinding>
</DataGridTextColumn.Width>
</DataGridTextColumn>
<DataGridTextColumn x:Name="StatValue" Binding="{Binding b}" Header="Val" Width="{Binding ElementName=Total, Path=ActualWidth}" />
</DataGrid.Columns>
<local:MyGenericRecord a="Min" b="2500" />
<local:MyGenericRecord a="Max" b="7500" />
<local:MyGenericRecord a="Average" b="5000" />
</DataGrid>
</StackPanel>
多重绑定尝试(代码部分)
public class WidthConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double totalWidth = 0;
foreach (DataGridLength Width in values)
totalWidth += Width.Value;
return totalWidth;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
推荐答案
代码最终将按预期执行以下操作:
The code finally executes as expected with the following:
-
< Binding Source = {x:Reference Samp2} Path = ActualWidth />
-
返回新的DataGridLength(totalWidth);
<Binding Source="{x:Reference Samp2}" Path="ActualWidth" />
return new DataGridLength(totalWidth);
转换器
在加载时和调整 Samp1大小时会被调用
或 Samp2
。列宽按预期保持同步。
The Converter
gets called on load and when resizing Samp1
or Samp2
. The column widths remain synchronized as expected.
<DataGridTextColumn x:Name="StatName" Binding="{Binding a}" Header="Stat">
<DataGridTextColumn.Width >
<MultiBinding Converter="{StaticResource WidthConverter}">
<Binding Source="{x:Reference Samp1}" Path="ActualWidth" />
<Binding Source="{x:Reference Samp2}" Path="ActualWidth" />
</MultiBinding>
</DataGridTextColumn.Width>
</DataGridTextColumn>
Convert
函数需要返回 DataGridLength
,数据类型为 DataGridTextColumn.Width
。
The Convert
function needed to return a DataGridLength
, the data type of DataGridTextColumn.Width
.
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double totalWidth = 0;
foreach (double Width in values)
totalWidth += Width;
return new DataGridLength(totalWidth);
}
注意:该代码将按预期执行,无论错误指定的转换无效。
Note: The code executes as expected, regardless of the error Specified cast is not valid.
- Visual Studio设计器强调整个标签
< MultiBinding ...< / MultiBinding>
用青色表示。 - 错误列表窗口报告错误指定的转换无效。
- 虽然显示为错误,但Visual Studio仍会生成并执行代码。
- The Visual Studio designer underlines the entire tag
<MultiBinding ... </MultiBinding>
in cyan. - The Error List window reports the error "Specified cast is not valid."
- While it is displayed as an error, Visual Studio will still build and execute the code.
这篇关于将DataGrid列宽绑定到另一个DataGrid的两个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!