WPF – 作为依赖对象的转换器



无法将值绑定到BindingConverterParametrBinding只能在DependencyObjectDependencyProperty上设置。

我很好奇IValueConverter转换器实现为DependencyObject.

public class AddConverter : DependencyObject, IValueConverter
{
public static readonly DependencyProperty AddIntegerProperty =
DependencyProperty.Register(nameof(AddInteger),
typeof(int),
typeof(AddConverter),
new PropertyMetadata(0));
public int AddInteger
{
get => (int)GetValue(AddIntegerProperty);
set => SetValue(AddIntegerProperty, value);
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is int intValue)) return 0;
return intValue + AddInteger;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
int intValue;
try
{
intValue = System.Convert.ToInt32(value);
}
catch (Exception)
{
return 0;
}
return intValue - AddInteger;
}
}

让我们在示例视图中使用它。

<TextBox>
<TextBox.Text>
<Binding Path="MyIntegerProperty">
<Binding.Converter>
<local:AddConverter AddInteger="{Binding MyAddIntegerProperty, Mode=OneWay}" />
</Binding.Converter>
</Binding>
</TextBox.Text>
</TextBox>

结果是AddInteger仍返回默认值。通过提供的Binding没有更改AddInteger依赖项属性的原因是什么?


脚注:MultiBinding对我没有帮助ConvertBack因为该方法仅由控件提供的值组成。这些东西也应该在 ViewModel 中解决,但我对带有转换器的解决方案感到好奇。

首先,问题是转换器无法继承它所在的 DataContext,因此绑定无法工作: 如果在绑定上放置跟踪,您将在 VS 输出中看到"找不到框架导师"(请参阅附录 A)。这也是为什么你不能只从FrameworkElement派生并使用RelativeSource={RelativeSource AncestorType=Whatever}:你已经脱离了可视化树。没有祖先。此外,即使有框架导师,依赖项对象也无法为绑定提供源。来源必须是明确的。只有从 FrameworkElement 继承的类才能继承 DataContext。

所以我偷了一个 BindingProxy 类(从这个答案中),并用它来为绑定提供源代码。这有点笨拙,但我想到的另一种选择是从 Freezable 继承转换器,本质上赋予它 BindingProxy 的属性,并在资源中创建转换器。它奏效了,但我更喜欢这种组织方式。

public class BindingProxy : Freezable
{
#region Overrides of Freezable
protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
}
#endregion
#region Data Property
public Object Data
{
get { return (Object)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register(nameof(Data), typeof(Object), typeof(BindingProxy),
new PropertyMetadata(null));
#endregion Data Property
}

XAML

<StackPanel.Resources>
<local:BindingProxy
x:Key="VMProxy"
Data="{Binding}"
/>
</StackPanel.Resources>
<TextBlock>
<TextBlock.Text>
<Binding Path="MyIntegerProperty">
<Binding.Converter>
<local:AddConverter
AddInteger="{Binding Data.MyAddIntegerProperty, Source={StaticResource VMProxy}}" 
/>
</Binding.Converter>
</Binding>
</TextBlock.Text>
</TextBlock>

附录A

AddInteger="{Binding MyAddIntegerProperty, Mode=OneWay, 
PresentationTraceSources.TraceLevel=High}"
System.Windows.Data Warning: 56 : Created BindingExpression (hash=14964341) for Binding (hash=21653700)
System.Windows.Data Warning: 58 :   Path: 'MyAddIntegerProperty'
System.Windows.Data Warning: 60 : BindingExpression (hash=14964341): Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=14964341): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=14964341): Attach to WpfApp2.AddConverter2.AddInteger (hash=57434139)
System.Windows.Data Warning: 64 : BindingExpression (hash=14964341): Use Framework mentor <null>
System.Windows.Data Warning: 67 : BindingExpression (hash=14964341): Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=14964341): Framework mentor not found
System.Windows.Data Warning: 65 : BindingExpression (hash=14964341): Resolve source deferred
System.Windows.Data Warning: 67 : BindingExpression (hash=14964341): Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=14964341): Framework mentor not found

最新更新