DataTrigger setter 只调用了一次

DataTrigger setter called only once

我有以下风格。我的问题是即使多重绑定总是正确的,我只在加载 Window 时观察 setter 一次。每当我更改 Window 高度时,这个 MyMultiValueConverter 被调用,我可以在日志中看到它,但看不到“”。

<Style x:Key="SeperatorRectangleStyle" TargetType="Rectangle">
    <Setter Property="Width" Value="2"/>
    <Setter Property="Height" Value="50"/>
    <Setter Property="MinHeight" Value="49"/>
    <Style.Triggers>
        <DataTrigger Value="True">
            <DataTrigger.Binding>
                <MultiBinding Converter="{StaticResource myTheMultiValueConverter}">
                    <Binding RelativeSource="{RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}" Path="ActualHeight"/>
                    <Binding RelativeSource="{RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}" Path="Name"/>
                </MultiBinding>
            </DataTrigger.Binding>
            <Setter Property="Height" Value="{Binding Converter={StaticResource mySeperatorHeightConverter}, ConverterParameter=DataTrigger}"/>

        </DataTrigger>
    </Style.Triggers>
</Style>

MyMultiValueConverter 是:

class TheMultiValueConverter : IMultiValueConverter
            {
                public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
                {
                   foreach (var i in values)
                  {
                      Console.WriteLine("TheMultiValueConverter values" + i);
                  }
                  Console.WriteLine("");
                   return true 
            }
}

MySeperatorHeightConverter 是

    class SeperatorHeightConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                Console.WriteLine("!-!- SeperatorHeightConverter: " + parameter);
                return 100;
            }
    }

您的代码中的问题是您将某些内容绑定到不会更改的高度。这就是第二个转换器没有被触发的原因。

查看此代码:

 <Setter Property="Height" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=ActualHeight, Converter={StaticResource mySeperatorHeightConverter}, ConverterParameter=DataTrigger}"/>

这个小改动使得 SeperatorHeightConverter 每次 Window 高度改变时都会被触发。