【发布时间】:2021-07-24 16:49:03
【问题描述】:
修改:很抱歉在第一版中遗漏了一些重要的描述,现在问题应该已经明确了:
所以我正在制作一个具有以下视图的玩具 CAD 程序:
- MainWindow.xaml
- CustomizedUserControl.xaml
CustomizedUserControl 是 MainWindow 中的一个 Tab,它的 DataContext 在 MainWindow.xaml 中定义为:
<Window.Resources>
<DataTemplate DataType="{x:Type local:CustomizedTabClass}">
<local:UserControl1/>
</DataTemplate>
</Window.Resources>
CustomizedUserControl.xaml 提供了一个画布和一个按钮,因此当按下按钮时,用户应该能够在画布上进行绘制。如下代码所示,Canvas的内容是由dataContext“tabs:CustomizedTabClass”准备的。
CustomizedUserControl.xaml
<CustomizedUserControl x:Name="Views.CustomizedUserControl11"
...
>
<Button ToolTip="Lines (L)" BorderThickness="2"
Command="{Binding ElementName=CustomizedUserControl11,
Path=DrawingCommands.LinesChainCommand}"
IsEnabled="True"
Content = "{Binding ElementName=CustomizedUserControl11,
Path=DrawingCommands.Button1Name}">
</Button>
...
<canvas x:Name="CADCanvas"
Drawing="{Binding Drawing ,Mode=TwoWay}" >
</canvas>
值得注意的是,我在所有类中都使用了外部库 Fody/PropertyChanged,因此无需进一步编程即可注入属性通知。
CustomizedUserControl.xaml.cs
using PropertyChanged;
using System.ComponentModel;
using System.Windows.Controls;
[AddINotifyPropertyChangedInterface]
public partial class CustomizedUserControl: Usercontrol, INotifyPropertyChanged{
public CADDrawingCommands DrawingCommands { get; set; }
public CustomizedUserControl()
{
InitializeComponent();
DrawingCommands = new CADDrawingCommands(this);
DrawingCommands.Button1Name = "yeahjojo"; //For testing data binding
}
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
}
CADDrawingCommands.cs
using PropertyChanged;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
[AddINotifyPropertyChangedInterface]
public class CADDrawingCommands : INotifyPropertyChanged{
UserControl _drawableTab;
public string Button1Name { get; set; } = "TestForDataBinding";
public RoutedCommand LinesChainCommand { get; set; } = new RoutedCommand();
public CADDrawingCommands(UserControl dTab){
_drawableTab = dTab;
CommandBinding lineCommandBinding = new CommandBinding(LinesChainCommand,
(object sender, ExecutedRoutedEventArgs e) =>
{
MessageBox.Show("Test");
//Draw on canvas inside CustomizedUserControl (modify Drawing property in CustomizedTabClass)
}, (object sender, CanExecuteRoutedEventArgs e) => { e.CanExecute = true; });
_drawableTab.CommandBindings.Add(lineCommandBinding);
}
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
}
Button 的内容设置正确,因为我可以读取 Button1Name 中定义的字符串:
因此,我认为命令的数据绑定也可以。 IsEnabled 已设置为 true,并且 CommandBinding 的 CanExecute 只会返回 true。
为什么我的按钮仍然是灰色且不可点击?
如果我在 Window 而不是 UserControl 中定义按钮(并将 Window 的 datacontext 设置为它自己的代码,按钮将是可点击的!为什么?
感谢您的宝贵时间!希望有人能帮助我,因为我的想法和参考资料已经用完了。
【问题讨论】:
-
您将 CommanBinding 添加到
_drawableTab元素的集合中。但是从你的代码中并不清楚这个元素是什么。 -
_drawable 是 CustomizedUserControl ,因为我想设置我的 UserControl 的 CommandBindings 属性,这样当按下 UserControl 中的 Button 时,用户可以在同样位于该 UserControl 的画布上画线。
标签: wpf button data-binding commandbinding routed-commands