c# - 将按钮命令绑定(bind)到非静态属性

标签 c# wpf mvvm commandbinding

我想将我的按钮绑定(bind)到非静态 ICommand 属性。但似乎我发现的所有示例都只与静态属性有关。

例如。这是我迄今为止所管理的

<Button Grid.Column="1" HorizontalAlignment="Right" Background="{DynamicResource Accent}" Margin="80,0"
        DataContext="{TemplateBinding LogoData}"
        Command="{Binding Source={x:Static materialMenu:SideMenu.OpenSidebar}}">
    <Path Data="{Binding}" Stretch="Fill" Fill="White" Width="24" Height="24" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Button>

上面的命令绑定(bind)只会让我绑定(bind)到静态命令属性。
下一个也是如此;
<Button Grid.Column="1" HorizontalAlignment="Right" Background="{DynamicResource Accent}" Margin="80,0"
        DataContext="{TemplateBinding LogoData}"
        Command="materialMenu:SideMenu.OpenSidebar"
        CommandParameter="{Binding}">
    <Path Data="{Binding}" Stretch="Fill" Fill="White" Width="24" Height="24" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Button>

所以我的问题是:由于我的数据上下文已经设置为另一个类,wpf 中是否有任何方法可以将命令绑定(bind)到非静态属性?

最佳答案

引用另一个包含 ICommand 的项目,

将 DataContext 设置为您的 ViewModel,

绑定(bind)命令为<Button Command="{Binding MyCustomCommand}" .../> ,因为 MyCustomCommand 是 ViewModel 中包含的命令的属性名称。

关于c# - 将按钮命令绑定(bind)到非静态属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34107199/

相关文章:

wpf - 如何将 ApplicationCommands 绑定(bind)到 ViewModel?

c# - 检测单击 TreeView 中的折叠按钮

c# - Linq to Xml,保持 XDocument 加载?

c# - 控制台应用程序中的 Application.Current "null"

wpf - 如何通过 AdornedElementPlaceholder 使用 TextTrimming 获取文本 block ?

c# - 如何在 mvvm 中使用复合命令

c# - 事件到命令行为不执行命令

c# - Unity3d 中的 Matrix.RotateAt

c# - 如何获取进程使用的CPU周期数

c# - 绑定(bind)到 WPF 中的另一个绑定(bind)