简体   繁体   English

绑定到模板中的嵌套属性

[英]Binding to a nested property in template

I want to create a button with loading indicator for async methods, like this: 我想为异步方法创建一个带加载指示符的按钮,如下所示:

在此输入图像描述

I create an AsyncCommand implementing ICommand and INotifyPropertyChanged (more details in MSDN Magazine April, 2014). 我创建了一个实现ICommandINotifyPropertyChangedAsyncCommand (更多细节见MSDN杂志2014年4月)。

My AsyncCommand has Execution property and it has IsNotCompleated property. 我的AsyncCommand具有Execution属性,它具有IsNotCompleated属性。 I want to set Visibility property of loader indincator using IsNotCompleted property. 我想设置Visibility装载机的财产indincator使用IsNotCompleted财产。

I create UserControl inherits the Button and set style for this. 我创建UserControl继承Button并为此设置样式。 I try to bind visibility to Button's Command dependency property. 我尝试将可见性绑定到Button的Command依赖项属性。

There the part of ControlTemplate with "Loading indicator" (I use content, not Visibility, just for example) ControlTemplate的一部分带有“加载指示器”(我使用内容,而不是可见性,仅作为示例)

 <Label HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Grid.Column="1"
        Content="{Binding Command.Execution.IsNotCompleted}"/>

And nothing happens. 没有任何反应。 If i use TemplateBinding instead of Binding I'll get compile errors: 如果我使用TemplateBinding而不是Binding我将得到编译错误:

  • Nested types not supported: Command.Execution 不支持嵌套类型:Command.Execution
  • Unable to find type Command.Execution 无法找到类型Command.Execution

What is the correct way to do this? 这样做的正确方法是什么?

The solution is using RelativeSource.TemplatedParent 解决方案是使用RelativeSource.TemplatedParent

<Label HorizontalAlignment="Center"
    VerticalAlignment="Center"
    Grid.Column="1"
    Content="{Binding Command.Execution.IsNotCompleted, RelativeSource={RelativeSource TemplatedParent}}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM