[英]WPF data-binding with multiple dependencies?
我相信這段代碼可能需要比平均水平更多的精力來理解,因此,我正在使用上下文代碼注釋來解釋我的困境,以使其更容易理解我的問題。
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; }
}
我認為您應該使用MultiValueConverter
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.