【发布时间】:2023-03-14 02:18:01
【问题描述】:
我在Grid 上使用MultiBinding 和MultiValueConverter,以便根据所有Grid.Rows 中的ActualHeight 设置每个Grid.Row 的MaxHeight。
<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>
多转换器:
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();
}
}
UserControl.Resources:
<cvc:AvailableHeightConverter x:Key="CalculateMaxHeightConverter" PanelHeight="1080" />
我的问题(一个假人)是,每当 Grid.Row 的 ActualHeight 被更改时,我无法弄清楚如何使用 UpdateSourceTrigger。
【问题讨论】:
-
尝试将接口 INotifyPropertyChanged 实现到您的类。
-
这会在某些属性发生变化时自动通知 UI
-
但在 MultiValueConverter 中不需要
-
但是您需要一些东西来触发 UpdateSource 吗?不是吗?
-
是的,但是,IMultiValueConverter 应该能够检测到作为
values[]传递的Bindings 的任何更改
标签: wpf grid height multibinding