我已经搜索了几个小时来解决这个问题。但没有运气。
我有 treeview (两个级别)。每个树视图项都包含checkbox
和textblock
。我添加了命令绑定到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可以正常工作。是否有任何其他控件可以使用而不是使用单击命令绑定的文本块。
修改
这是我已经从我的viewmodel
RelayCommand _productSelectCommand;
public ICommand ProductSelectCommand
{
get
{
if ( _productSelectCommand == null)
{
_productSelectCommand = new RelayCommand(param => ProductSelect((string)param));
}
return _productSelectCommand ;
}
}
public void ProductSelect(string productName)
{
}
答案 0 :(得分:0)
如果您使用RelayCommand
中的MVVM Light Tollkit
,则可以使用此项:
if ( _productSelectCommand == null)
{
_productSelectCommand =
new RelayCommand(
param => ProductSelect((string)param),
p => true
);
}
或在运行命令时实现与逻辑相关的信息。