Question: How do I bind StatName.Width
to Samp1.ActualWidth + Samp2.ActualWidth
?
Sub-questions:
Why are my WidthConverter
functions never executed (breakpoints never hit)?
What is the right <Binding ...
syntax for the individual MultiBinding components?
What I've tried:
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.
EDIT
With using <Binding ElementName="Samp1" Path="ActualWidth" />
- No build errors or warnings.
- The
Convert
function is not hit - No relevant messages in Output
With using <Binding Source="{x:Reference Samp1}" Path="ActualWidth" />
- 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 Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='40' MultiBindingExpression:target element is 'DataGridTextColumn' (HashCode=64879470); target property is 'Width' (type 'DataGridLength')
MultiBinding Attempt (xaml part)
<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>
MultiBinding Attempt (code part)
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;
}
}
ActualWidth
isn't adouble
? I tried and change formDataGridLength
todouble
and it worked. – Denominationalforeach ( double Width in values ) totalWidth += Width;
. The code compiled and ran, but theStatName
column was still only as wide as the word Average – SelimahWidthConverter
function is never called. Do you know why? Also, did my code sample execute correctly for you? – SelimahOutput
window for binding errors. – YellowbirdSpecified cast is not valid
on my MultiBinding statement. Do you happen to know why? – Selimah