【问题标题】:Multibinding using Window's DataContext使用 Window 的 DataContext 进行多重绑定
【发布时间】:2012-12-26 00:22:36
【问题描述】:

我正在尝试根据窗口的Customer DependancyProperty 是否为空来创建拖动装饰器。我在Window的资源中有这个。绑定的第一部分已设置(被拖动的项目),但第二部分(窗口上的 DependancyProperty)显示为 UnsetValue。

该属性肯定被初始化,因为它被用作窗口的数据上下文。

<Window x:Name="root"
    ...
>
<Window.Resources>
    <DataTemplate x:Key="DragAdorner">
        <StackPanel Orientation="Horizontal">
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding Converter="{StaticResource vehicleDragConverter}">
                        <Binding/>
                        <Binding Path="Customer" ElementName="root"/>
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </StackPanel>
</DataTemplate>

【问题讨论】:

    标签: c# .net wpf dependency-properties datacontext


    【解决方案1】:

    customer 不是元素窗口的直接属性。

    1. 使用&lt;Binding Path="DataContext.Customer"..../&gt;

    或者你也可以在绑定中使用RelativeSource=FindControl Window。

    【讨论】:

    • RelativeSource 不起作用,使用 DataContext.Customer:Cannot find source for binding with reference 'ElementName=root'... 时我仍然得到未设置的值
    • 在RelativeResource中使用AncestorType
    • 你已经在代码中设置了DataContext = this?因为它应该可以工作。
    猜你喜欢
    • 2014-07-20
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 2011-10-04
    相关资源
    最近更新 更多