早上好,就我对绑定的理解而言,似乎已经打了一堵砖墙 . 我是WPF的新手,一般是在xaml中绑定 . 我正在尝试获取树视图中的按钮以在ItemTree视图模型中执行命令以将项目绘制到画布...但是我没有那么远 .
我不一定会选择“按钮”,但我想确保我对Command的理解是明确的......显然它不是!
树的模型是ItemsGroup,它包含Name,Path,Children ......等 .
错误消息是它无法在ItemsGroup模型中找到该命令 . 我试图将HierarchicalDataTemplate的DataType更改为ViewModel,但这也不起作用 .
RelayCommand主要是样板继承ICommand的类 . 但是,它没有达到它,因为它是混乱的搞砸了 .
有没有人有什么建议?
窗口:
public partial class ViewMain : Window
{
public ViewMain()
{
InitializeComponent();
DataContext = new ViewModelMain();
ItemTree.DataContext = new ViewModelItemTree();
...
}
}
窗口xaml:
<ScrollViewer Grid.Row="1" Grid.Column="0">
<TreeView x:Name="ItemTree" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Binding Children}" Height="334" Width="192">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type model:ItemGroups}">
<Button Content="{Binding Name}" Command="{Binding DrawItemsCommand}" CommandParameter="{Binding Path}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</ScrollViewer>
ViewModel(ViewModelItemTree):
public RelayCommand DrawItemsCommand { get; set; }
void ExecuteDrawItemsCommand(object parameter)
{
if (parameter == null) return;
...
}
public ViewModelItemTree()
{
List<string> itemsList = DataLayer.GetItemsForTree();
Children = new ObservableCollection<ItemGroups>(TreeLoad(itemsList));
DrawItemsCommand = new RelayCommand(ExecuteDrawItemsCommand);
}
错误:
System.Windows.Data Error: 40 : BindingExpression path error: 'DrawItemsCommand' property not found on 'object' ''ItemGroups' (HashCode=35751240)'. BindingExpression:Path=DrawItemsCommand; DataItem='ItemGroups' (HashCode=35751240); target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand')