【问题标题】:WPF Treeview with HierarchicalDataTemplate - items not selectable带有 HierarchicalDataTemplate 的 WPF 树视图 - 项目不可选择
【发布时间】:2015-01-28 14:17:10
【问题描述】:

我已经搜索了几个小时来解决这个问题。但没有运气。

我有 treeview(两个级别)。每个树视图项包含checkboxtextblock。我已将命令绑定添加到文本块,添加后 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 可能对我从后面的代码中使用的命令提出质疑

标签: wpf treeview


【解决方案1】:

如果您使用来自MVVM Light TollkitRelayCommand,您可以使用这个:

if ( _productSelectCommand == null)
{
     _productSelectCommand = 
                new RelayCommand(
                                   param => ProductSelect((string)param),
                                   p => true
                                );
}

或者当命令可以运行时实现你的相关逻辑。

【讨论】:

  • 做到了。但仍然没有运气:(
  • Binding DataContext.ProductCategoriesSelectCommand 看起来很奇怪。检查输出窗口中的绑定错误。你在哪个类中实现了ProductCategoriesSelectCommand?我认为您必须改用Binding ProductCategoriesSelectCommand
  • 对不起。我没明白你的问题。你的意思是不使用MouseBinding 使用binding?如果是这样,则不支持绑定。你能给我一个样品吗
  • 请注意:此命令绑定工作正常。当前问题是当我单击树视图项目时,该项目未被选中。其他命令执行正常
猜你喜欢
  • 2020-09-05
  • 1970-01-01
  • 2013-08-10
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
  • 1970-01-01
  • 1970-01-01
  • 2017-08-27
相关资源
最近更新 更多