我在MultiBinding
上使用MultiValueConverter
和Grid
,以便根据MaxHeight
设置每个Grid.Row
的{{1}}所有ActualHeight
s。
Grid.Row
多转换:
<RowDefinition x:Name="grdRow1" Height="Auto" >
<RowDefinition.MaxHeight>
<MultiBinding Converter="{StaticResource CalculateMaxHeightConverter}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding Path="ActualHeight" ElementName="grdRow1" />
<Binding Path="ActualHeight" ElementName="grdRow2" />
<Binding Path="ActualHeight" ElementName="grdRow3" />
<Binding Path="ActualHeight" ElementName="grdRow4" />
<Binding Path="ActualHeight" ElementName="grdRow5" />
</MultiBinding>
</RowDefinition.MaxHeight>
</RowDefinition>
UserControl.Resources:
public class AvailableHeightConverter : IMultiValueConverter
{
public double PanelHeight { get; set; }
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null)
return Binding.DoNothing;
double currentHeight = 0d;
double availableHeight = 0d;
foreach (object value in values)
{
currentHeight += (double)value;
}
availableHeight = PanelHeight - currentHeight;
if (availableHeight < 0)
return Binding.DoNothing;
return availableHeight;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
我的问题(一个假的问题)是,当<cvc:AvailableHeightConverter x:Key="CalculateMaxHeightConverter" PanelHeight="1080" />
的{{1}}被更改时,我无法弄明白UpdateSourceTrigger
的方式。
答案 0 :(得分:0)
你的xaml有两个问题(据我所知,我自己没有尝试过)
尝试以下方法:
<RowDefinition x:Name="grdRow1" Height="Auto" >
<RowDefinition.MaxHeight>
<MultiBinding Converter="{StaticResource CalculateMaxHeightConverter}" Mode="OneWay" UpdateSourceTrigger="PropertyChanged">
<Binding Path="ActualHeight" ElementName="grdRow2" Mode="OneWay" />
<Binding Path="ActualHeight" ElementName="grdRow3" Mode="OneWay" />
<Binding Path="ActualHeight" ElementName="grdRow4" Mode="OneWay" />
<Binding Path="ActualHeight" ElementName="grdRow5" Mode="OneWay" />
</MultiBinding>
</RowDefinition.MaxHeight>
</RowDefinition>
答案 1 :(得分:0)
行。在尝试各种senarios后,我最终Binding
行ActualHeight
没有通知MultiBinding
更改。 @christoph注意到(2.)也非常有帮助。所以我最终绑定了TextBox
&#39; ActualWidth
,如下所示:
<RowDefinition x:Name="grdRow1" Height="Auto" >
<RowDefinition.MaxHeight>
<MultiBinding Converter="{StaticResource CalculateMaxHeightConverter}">
<Binding Path="ActualHeight" ElementName="txtBox2" Mode="OneWay" />
<Binding Path="ActualHeight" ElementName="txtBox3" Mode="OneWay" />
<Binding Path="ActualHeight" ElementName="txtBox4" Mode="OneWay" />
<Binding Path="ActualHeight" ElementName="txtBox5" Mode="OneWay" />
</MultiBinding>
</RowDefinition.MaxHeight>
</RowDefinition>
第2行的
<RowDefinition x:Name="grdRow2" Height="Auto" >
<RowDefinition.MaxHeight>
<MultiBinding Converter="{StaticResource CalculateMaxHeightConverter}">
<Binding Path="ActualHeight" ElementName="txtBox1" Mode="OneWay" />
<Binding Path="ActualHeight" ElementName="txtBox3" Mode="OneWay" />
<Binding Path="ActualHeight" ElementName="txtBox4" Mode="OneWay" />
<Binding Path="ActualHeight" ElementName="txtBox5" Mode="OneWay" />
</MultiBinding>
</RowDefinition.MaxHeight>
</RowDefinition>
并从Height
(PanelHeight
)中减去一行的AvailableHeightConverter
。
<cvc:AvailableHeightConverter x:Key="CalculateMaxHeightConverter" PanelHeight="1028" />
(曾经是1080(-52 MinHeight
行)