【问题标题】:Using a WPF controls own properties in Command Binding在命令绑定中使用 WPF 控件自己的属性
【发布时间】:2010-02-23 09:25:28
【问题描述】:

我有一个切换按钮。我正在使用命令绑定,并且我想将其 IsChecked 属性的值作为参数传递。如果不命名 ToggleButton 并使用其名称来解决自身问题,我该如何做到这一点?

目前我正在通过命名控件来解决这个问题,但我认为这可以做得更好吗?

<ToggleButton x:Name="_myToggle" 
              Command="{Binding SomeCommand}" 
              CommandParameter="{Binding ElementName=_myToggle, Path=IsChecked}">
    Apply Toggle
</ToggleButton>

【问题讨论】:

    标签: .net wpf commandbinding


    【解决方案1】:

    你需要使用自绑定:

    <ToggleButton x:Name="_myToggle" 
                  Command="{Binding SomeCommand}" 
                  CommandParameter="{Binding RelativeSource={RelativeSource Self},
                                             Path=IsChecked}">
        Apply Toggle
    </ToggleButton>
    

    希望这会有所帮助!

    【讨论】:

    • 当然!谢谢!以前做过,但我在那里完全忘记了一段时间..
    猜你喜欢
    • 2014-05-10
    • 2010-12-26
    • 2011-08-18
    • 1970-01-01
    • 2018-04-09
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    相关资源
    最近更新 更多