WPF数据绑定多个依赖项?

时间:2009-09-11 06:16:02

标签: c# .net wpf data-binding xaml

我认为这段代码可能需要更多的理解而不是平均值,因此我使用上下文代码注释来解释我的困境,以便更容易理解我的问题。

public class MyControl : System.Windows.FrameworkElement
{
    public double Property1 { get; set; }
    public double Property2 { get; set; }
}

...

<StackPanel>
    <local:MyControl Name="ControlA" />
    <local:MyControl Name="ControlB" />
    <!--We want to use data-binding to force ControlA.MinWidth to be equal to
        ControlA.Property1 * ControlB.Property2 at all times-->
</StackPanel>

...

static readonly MyPrimaryConverter PrimConv = new MyPrimaryConverter();
static readonly MySecondaryConverter SecConv = new MySecondaryConverter();

public Window1()
{
    InitializeComponent();

    System.Windows.Data.BindingExpressionBase BindExp1, BindExp2;
    System.Windows.Data.Binding MyBinding;

    MyBinding = new System.Windows.Data.Binding("Property1");
    MyBinding.Source = ControlA;
    MyBinding.Mode = System.Windows.Data.BindingMode.OneWay;
    MyBinding.Converter = PrimConv;
    MyBinding.ConverterParameter = ControlB;
    BindExp1 = ControlA.SetBinding(
                        System.Windows.UIElement.MinWidthProperty, MyBinding);
    //Binds correctly and shows "Status" as active

    MyBinding = new System.Windows.Data.Binding("Property2");
    MyBinding.Source = ControlB; // ControlB.Property2 is the source value now
    MyBinding.Mode = System.Windows.Data.BindingMode.OneWay;
    MyBinding.Converter = SecConv; //Uses the secondary converter
    MyBinding.ConverterParameter = ControlA; //ControlA is the parameter now
    BindExp2 = ControlA.SetBinding(
                        System.Windows.UIElement.MinWidthProperty, MyBinding);
    /* Also binds correctly and shows "Status" as active,
       but causes BindExp1 to show "Status" as "Detached"

       ****THIS IS THE PROBLEM I NEED HELP WITH****

       How can I successfully bind ControlA.MinWidth to BOTH of the values
       that are used to calculate MinWidth?

       This is required so that MinWidth will be recalculated if EITHER
       value changes at any time

       Bonus points if you can show how to do it with
       only one converter class instead of two */
}

...

[System.Windows.Data.ValueConversion(typeof(double), typeof(double))]
public class MyPrimaryConverter : System.Windows.Data.IValueConverter
{
    public object Convert(  object value, System.Type targetType,
                      object parameter, System.Globalization.CultureInfo culture)
    {
        if ((value is double) && (parameter is MyControl))
            return ((double)value) * (parameter as MyControl).Property2;
            /* This converter ALWAYS returns:
               ControlA.Property1 * ControlB.Property2
               value == ControlA.Property1 and parameter == ControlB,
               so we return value * parameter.Property2 */

        return System.Windows.DependencyProperty.UnsetValue;
    }

    public object ConvertBack(  object value, System.Type targetType,
                      object parameter, System.Globalization.CultureInfo culture)
    {   return System.Windows.DependencyProperty.UnsetValue;    }
}

[System.Windows.Data.ValueConversion(typeof(double), typeof(double))]
public class MySecondaryConverter : System.Windows.Data.IValueConverter
{
    public object Convert(  object value, System.Type targetType,
                      object parameter, System.Globalization.CultureInfo culture)
    {
        if ((value is double) && (parameter is MyControl))
            return ((double)value) * (parameter as MyControl).Property1;
            /* This converter also ALWAYS returns:
               ControlA.Property1 * ControlB.Property2
               "value" and "parameter" are swapped:
               value == ControlB.Property2 and parameter == ControlA
               so we return value * parameter.Property1
               instead of value * parameter.Property2 */

        return System.Windows.DependencyProperty.UnsetValue;
    }

    public object ConvertBack(  object value, System.Type targetType,
                      object parameter, System.Globalization.CultureInfo culture)
    {   return System.Windows.DependencyProperty.UnsetValue;    }
}

1 个答案:

答案 0 :(得分:3)

我认为你应该使用MultiValueConverter