wpf - Dependencia de la propiedad dependiente de otro.
dependency-properties linkage (2)
¿Cómo se puede registrar una propiedad de dependencia cuyo valor se calcula utilizando el valor de otra propiedad de dependencia?
Debido a que WPF omite los envoltorios de propiedades .NET en tiempo de ejecución, no se debe incluir la lógica en los captadores y definidores. La solución a esto es típicamente usar PropertyChangedCallback
s. Pero esos son declarados estáticos.
Por ejemplo, cuál es la forma correcta de realizar esta tarea artificial:
public bool TestBool
{
get { return (bool)GetValue(TestBoolProperty); }
set
{
SetValue(TestBoolProperty, value);
TestDouble = ((value)?(100.0):(200.0)); // HERE IS THE DEPENDENCY
}
}
public static readonly DependencyProperty TestBoolProperty =
DependencyProperty.Register("TestBool", typeof(bool), typeof(ViewModel));
public double TestDouble
{
get { return ((double)GetValue(TestDoubleProperty)); }
set { SetValue(TestDoubleProperty, value); }
}
public static readonly DependencyProperty TestDoubleProperty =
DependencyProperty.Register("TestDouble", typeof(double), typeof(ViewModel));
Mientras la dependencia no sea circular, ¿existe un medio adecuado para lograr esto?
En realidad estás en lo correcto, debes usar PropertyChangedCallback. Así es cómo:
public bool TestBool
{
get { return (bool)GetValue(TestBoolProperty); }
set
{
SetValue(TestBoolProperty, value);
}
}
public static readonly DependencyProperty TestBoolProperty =
DependencyProperty.Register("TestBool", typeof(bool), typeof(ViewModel),
new PropertyMetadata(false, new PropertyChangedCallback(OnTestBoolChanged)));
private static void OnTestBoolChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ViewModel vm = d as ViewModel;
vm.TestDouble = value ? 100.0 : 200.0;
}
public double TestDouble
{
get { return ((double)GetValue(TestDoubleProperty)); }
set { SetValue(TestDoubleProperty, value); }
}
public static readonly DependencyProperty TestDoubleProperty =
DependencyProperty.Register("TestDouble", typeof(double), typeof(ViewModel));
Hmmm ... Creo que deberías mirar las propiedades de dependencia valor coerción . Aquí hay un ejemplo con coerción:
public class ViewModel : DependencyObject
{
public bool TestBool
{
get { return (bool)GetValue(TestBoolProperty); }
set { SetValue(TestBoolProperty, value); }
}
public static readonly DependencyProperty TestBoolProperty =
DependencyProperty.Register("TestBool", typeof(bool), typeof(ViewModel), new PropertyMetadata(false, OnTestBoolPropertyChanged));
private static void OnTestBoolPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var vm = (ViewModel)d;
vm.CoerceValue(TestDoubleProperty);
}
public double TestDouble
{
get { return ((double)GetValue(TestDoubleProperty)); }
set { SetValue(TestDoubleProperty, value); }
}
public static readonly DependencyProperty TestDoubleProperty =
DependencyProperty.Register("TestDouble", typeof(double), typeof(ViewModel), new PropertyMetadata(0.0, null, OnCoerceTestDouble));
private static object OnCoerceTestDouble(DependencyObject d, object baseValue)
{
var vm = (ViewModel) d;
var testBool = vm.TestBool;
return ((testBool) ? (100.0) : (200.0));
}
}