将路线/隧道属性值路由到具有行为的另一个属性

时间:2018-07-13 07:39:54

标签: wpf behavior

我有一个DataGrid,想将AlternationIndex传递到绑定元素(当前对象之外)。

CellTemplate

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <Grid>
            <TextBlock HorizontalAlignment="Center" Text="{Binding Index}"/>
            <i:Interaction.Behaviors>
                <behaviors:RoutePropertyValue 
                    Source="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=(ItemsControl.AlternationIndex)}"
                    Target="{Binding Index, Mode=TwoWay}"/>
            </i:Interaction.Behaviors>
        </Grid>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

* RoutePropertyValueBehavior **

public class RoutePropertyValue : Behavior<FrameworkElement>
{
    /// <summary>
    /// The source property value
    /// </summary>
    public object Source
    {
        get => (object)GetValue(SourceProperty);
        set => SetValue(SourceProperty, value);
    }

    /// <summary>
    /// The <see cref="Source"/> DependencyProperty.
    /// </summary>
    public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(object), typeof(RoutePropertyValue), new PropertyMetadata(null, SourceChangedCallback));

    private static void SourceChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs args)
    {
        if (d is RoutePropertyValue instance)
        {
            instance.OnSourceChanged();
        }
    }

    protected virtual void OnSourceChanged()
    {
        Target = Source;
    }


    /// <summary>
    /// The target where the value should be writetn
    /// </summary>
    public object Target
    {
        get => (object)GetValue(TargetProperty);
        set => SetValue(TargetProperty, value);
    }

    /// <summary>
    /// The <see cref="Target"/> DependencyProperty.
    /// </summary>
    public static readonly DependencyProperty TargetProperty = DependencyProperty.Register("Target", typeof(object), typeof(RoutePropertyValue), new PropertyMetadata(null));

}

实际上,调试时Target始终是null。同样,如果更改DataGrid内部的顺序,则不会再次调用SourceChangedCallback。因此,无论如何,将属性值“隧道”到另一个属性似乎是错误的方法。

1 个答案:

答案 0 :(得分:1)

您可以绑定到目标对象,然后指定要设置的源属性。每当设置了任何目标属性时,都应尝试设置source属性。试试这个:

public class RoutePropertyValue : Behavior<FrameworkElement>
{
    public object Source
    {
        get => GetValue(SourceProperty);
        set => SetValue(SourceProperty, value);
    }

    public static readonly DependencyProperty SourceProperty = DependencyProperty.Register(nameof(Source), typeof(object), 
        typeof(RoutePropertyValue), new PropertyMetadata(null, Callback));

    private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs args)
    {
        if (d is RoutePropertyValue instance)
            instance.OnAnyValueChanged();
    }

    protected virtual void OnAnyValueChanged()
    {
        if (!string.IsNullOrEmpty(TargetMember))
            TargetObject?.GetType().GetProperty(TargetMember)?.SetValue(TargetObject, Source);
    }

    public object TargetObject
    {
        get => GetValue(TargetObjectProperty);
        set => SetValue(TargetObjectProperty, value);
    }

    public static readonly DependencyProperty TargetObjectProperty = DependencyProperty.Register(nameof(TargetObject), typeof(object), 
        typeof(RoutePropertyValue), new PropertyMetadata(null, Callback));

    public string TargetMember
    {
        get => (string)GetValue(TargetMemberProperty);
        set => SetValue(TargetMemberProperty, value);
    }

    public static readonly DependencyProperty TargetMemberProperty = DependencyProperty.Register(nameof(TargetMember), typeof(string),
        typeof(RoutePropertyValue), new PropertyMetadata(null, Callback));
}

XAML:

<behaviors:RoutePropertyValue 
    TargetObject="{Binding}"
    TargetMember="Index"
    Source="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=(ItemsControl.AlternationIndex)}"
    />