【问题标题】:Reactive dependency property when multibinding多重绑定时的反应性依赖属性
【发布时间】:2013-01-14 07:25:57
【问题描述】:

我正在尝试将标签绑定到两个属性以对第二个属性的更改做出反应。

这背后的基础是在公制和英制之间切换时更改字段的单位标签:

Image http://s7.postimage.org/wmw3vofmj/test.png

XAML 代码如下:

<Label Name="UnitLabel" Grid.Column="1" Foreground="#FF1414AA" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Padding="2,2,5,2">
    <Label.Content>
        <MultiBinding Converter="{units:UnitConvertor}" Mode="OneWay">
            <Binding Path="Unit" />
            <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=base:MainForm, AncestorLevel=3}" Path="Document.Units" />
        </MultiBinding>
    </Label.Content>
</Label>

标签托管在用户控件上,实际文档有很多:

Image http://s9.postimage.org/47hvp9uyn/test.png

Document 对象如下图所示:

public class Document : DependencyObject
{
    public static readonly DependencyProperty UnitsProperty =
        DependencyProperty.Register("Units", typeof (UnitSet), typeof (Document), new PropertyMetadata(default(UnitSet)));

    public UnitSet Units
    {
        get { return (UnitSet) GetValue(UnitsProperty); }
        set { SetValue(UnitsProperty, value); }
    }

    public Document()
    {
        Units = UnitSet.Imperial;
    }
}

然后我尝试设置 Units 属性:

private void ChangeUnit(object sender, RoutedEventArgs e)
{
    if (sender == Metric)
    {
        Document.Units = UnitSet.Metric;
    }
    else if (sender == Imperial)
    {
        Document.Units = UnitSet.Imperial;
    }
}

但是,单位标签无法更新。我需要做什么来解决这个问题?抱歉,如果遗漏了任何明显的内容,我两天前开始使用 WPF。

【问题讨论】:

  • 多重绑定中的第一个绑定是什么?
  • 嗨,Nakiya,它只是在用户控件上定义的标准依赖属性

标签: c# .net wpf multibinding


【解决方案1】:

我不知道您的完整 XAML 树,但我认为您的 AncestorLevel 不正确。我猜 MainForm 是您的根元素,但您在该绑定中说您正在寻找该标签上方的第三个 MainForm 。尝试完全消除 AncestorLevel。默认为 1。

【讨论】:

  • 或者,只绑定ElementName属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-22
  • 2012-11-08
  • 2012-08-01
  • 2017-12-24
  • 2014-08-31
  • 2012-09-28
  • 2023-03-26
相关资源
最近更新 更多