我想将我的按钮绑定(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/