2009-09-11 54 views
0

我相信这段代码可能需要更多的努力才能理解,所以我使用上下文代码注释来解释我的困境,努力使它更容易理解我的问题。WPF与多个依赖关系的数据绑定?

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; } 
} 
+0

问题被嵌入代码 – Natrium 2009-09-11 06:25:26

回答

3

我认为你应该使用一个MultiValueConverter

+0

你的回答缺乏细节的意见 - 但IMultiValueConverter接口,配合到位的简单绑定的MultiBinding对象相结合,做的工作做好。 – Giffyguy 2009-09-11 09:25:22