【发布时间】:2015-01-28 14:17:10
【问题描述】:
我已经搜索了几个小时来解决这个问题。但没有运气。
我有 treeview(两个级别)。每个树视图项包含checkbox 和textblock。我已将命令绑定添加到文本块,添加后 treeviewitem 不可选择。
如何解决这个问题。
<TreeView ItemsSource="{Binding ProductCategories, Mode=TwoWay}">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True" />
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Products}">
<StackPanel Orientation="Horizontal" Margin="0,5,0,0">
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Command="{Binding DataContext.ProductCategoriesCheckedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
</CheckBox>
<TextBlock Margin="5,0,0,0" Text="{Binding Category}" >
<TextBlock.InputBindings>
<MouseBinding Command="{Binding DataContext.ProductCategoriesSelectCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" MouseAction="LeftClick" CommandParameter ="{Binding Category}" />
</TextBlock.InputBindings>
</TextBlock>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,5,0,0">
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Command="{Binding DataContext.ProductCheckedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
</CheckBox>
<TextBlock Margin="5,0,0,0" Text="{Binding Product.Name}">
<TextBlock.InputBindings>
<MouseBinding Command="{Binding DataContext.ProductSelectCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" MouseAction="LeftClick" CommandParameter ="{Binding Product.Name}" />
</TextBlock.InputBindings>
</TextBlock>
</StackPanel>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
没有 MouseBinding 的 Textbolck 可以正常工作。是否有任何其他控件可以用来代替带有单击命令绑定的文本块。
编辑
这是我从我的视图模型中实现的命令
RelayCommand _productSelectCommand;
public ICommand ProductSelectCommand
{
get
{
if ( _productSelectCommand == null)
{
_productSelectCommand = new RelayCommand(param => ProductSelect((string)param));
}
return _productSelectCommand ;
}
}
public void ProductSelect(string productName)
{
}
【问题讨论】:
-
你正确实现
ICommand.CanExecute了吗? -
对于命令
ProductSelectCommand? -
@HamletHakobyan-have updated 可能对我从后面的代码中使用的命令提出质疑