使用telerik radtreecontrol双击MVVM绑定方法

fly*_*lip 6 c# wpf xaml telerik mvvm

我一直在努力解决这个问题.尽管我内心的人说"不要这样做",现在是时候问路了.

我使用MVVM设计模式在WPF C#中编码.除非没有选择,否则我们会严格遵守模式并在代码中不加任何内容,否则这样做是完全不合理的.话虽如此,我正在使用Telerik RadTreeView.以下是我的XAML中的一小段内容:

<telerik:RadTreeView IsExpandOnSingleClickEnabled="True" IsLineEnabled="True" Margin="5" 
                                 ItemsSource="{Binding ItemsView}"
                                 SelectedItem="{Binding SelectedWidget, Mode=TwoWay}"
                                 ItemTemplate="{StaticResource NodeTemplate}" />
Run Code Online (Sandbox Code Playgroud)

目前树正常工作,因此如果您突出显示树项并单击视图上的"确定"按钮,则一切都很好.但是,我还需要允许用户双击其中一个树项.这意味着我已经在我的视图模型中有一个命令和方法,受保护的覆盖void OkAction(),具有所需的逻辑.Telerik提供了一个名为ItemDoubleClick的属性,该属性应该为树项双击提供功能.但我找不到任何允许我在视图模型中执行此操作的内容.换句话说,我该如何进行绑定?我们的项目中还有一个行为设置,用于双击,我被告知可以使用,但我没有行为经验.WPF我还是有点湿.

如果有帮助,这里是Telerik文档的链接:http://www.telerik.com/help/wpf/radtreeview-events-overview.html

我将不胜感激,任何人都可以提供帮助或指导.

试试看斯坦:

<Grid.Resources>
            <DataTemplate x:Key="WidgetTemplate">
                <StackPanel Orientation="Horizontal">
                    <Image Source="/Resources/gear1.png" Margin="1" Stretch="None" />
                    <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Margin="6,0,0,0" />
                </StackPanel>
            </DataTemplate>

            <HierarchicalDataTemplate x:Key="NodeTemplate" ItemsSource = "{Binding Children}" ItemTemplate="{StaticResource WidgetTemplate}">
                <TextBlock Text="{Binding Name}"/>
            </HierarchicalDataTemplate>

        </Grid.Resources>
Run Code Online (Sandbox Code Playgroud)

bme*_*ith 4

您可能希望在此处使用 DoubleClick 已有的附加行为。

否则,这里是我使用的完整代码,它创建附加行为,并将创建两个绑定到命令和可选命令参数的附加属性。

AttachedBehaviors.cs

public static class MouseDoubleClick
{
    public static DependencyProperty CommandProperty = 
        DependencyProperty.RegisterAttached("Command", 
            typeof(ICommand), 
            typeof(MouseDoubleClick), 
            new UIPropertyMetadata(CommandChanged));

    public static DependencyProperty CommandParameterProperty = 
        DependencyProperty.RegisterAttached("CommandParameter", 
            typeof(object), 
            typeof(MouseDoubleClick), 
            new UIPropertyMetadata(null));

    public static void SetCommand(DependencyObject target, ICommand value)
    {
        target.SetValue(CommandProperty, value);
    }

    public static void SetCommandParameter(DependencyObject target, object value)
    {
        target.SetValue(CommandParameterProperty, value);
    }
    public static object GetCommandParameter(DependencyObject target)
    {
        return target.GetValue(CommandParameterProperty);
    }

    private static void CommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
    {
        Control control = target as Control;
        if (control != null)
        {
            if ((e.NewValue != null) && (e.OldValue == null))
            {
                control.MouseDoubleClick += OnMouseDoubleClick;
            }
            else if ((e.NewValue == null) && (e.OldValue != null))
            {
                control.MouseDoubleClick -= OnMouseDoubleClick;
            }
        }
    }

    private static void OnMouseDoubleClick(object sender, RoutedEventArgs e)
    {
        Control control = sender as Control;
        ICommand command = (ICommand)control.GetValue(CommandProperty);
        object commandParameter = control.GetValue(CommandParameterProperty);
        if (command.CanExecute(commandParameter))
            command.Execute(commandParameter);
    }
}
Run Code Online (Sandbox Code Playgroud)

.xaml - 请记住添加附加行为所在的命名空间。

<telerik:RadTreeView IsExpandOnSingleClickEnabled="True" 
                     IsLineEnabled="True" 
                     Margin="5" 
                     ItemsSource="{Binding ItemsView}"
                     SelectedItem="{Binding SelectedWidget, Mode=TwoWay}"
                     ItemTemplate="{StaticResource NodeTemplate}"
                     acb:MouseDoubleClick.Command="{Binding ShowItemCommand}" />
Run Code Online (Sandbox Code Playgroud)

示例ViewModel.cs

private RelayCommand _showItemCommand;
public RelayCommand ShowItemCommand
{
    get
    {
        return _showItemCommand ?? (_showItemCommand =
            new RelayCommand(ShowItemDetails, IsItemSelected));
    }
}
Run Code Online (Sandbox Code Playgroud)