DataTrigger设置器仅调用一次

时间:2018-07-26 22:54:53

标签: wpf binding setter datatrigger

我有以下这种风格。我的问题是,即使多重绑定始终是正确的,加载Window时,我只能观察到setter一次。每当我更改“窗口高度”时,都会调用此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;
            }
    }

1 个答案:

答案 0 :(得分:0)

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

查看此代码:

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

这个很小的变化使得SeperatorHeightConverter每次窗口高度变化时都会被触发。