MVVM结合双击方法使用Telerik的radtreecontrol [英] MVVM binding double click to method using telerik radtreecontrol

查看:320
本文介绍了MVVM结合双击方法使用Telerik的radtreecontrol的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直对这个问题的时间愚蠢的量。现在是时候尽管我内心的人说要问路不这样做。



我使用MVVM设计模式编码WPF C#。我们尽量严格遵守模式,把没有在后面的代码,除非没有选择,或者是完全不合理的,这样做。话虽如此,我与一个Telerik的RadTreeView工作。这里是它在我的XAML代码片段:

 < Telerik的:RadTreeView IsExpandOnSingleClickEnabled =真IsLineEnabled =真保证金=5
的ItemsSource ={结合ItemsView}
的SelectedItem ={结合SelectedWidget,模式=双向}
的ItemTemplate ={StaticResource的NodeTemplate}/>

目前树是否工作正常,这样,如果你选中一个树项,然后单击确定按钮看来,一切都很好。但是,我还需要允许用户双击树中的项目之一。这意味着我已经有一个命令和方法,的保护覆盖无效OkAction()的,在我的要求的逻辑视图模型。 Telerik的提供了一个叫做物业的 ItemDoubleClick 的是应该为树项目双击提供的功能。但我无法找到任何东西,让我这样做的视图模型。换句话说,我怎么绑定?我们也有我们为我被告知我可以用双击项目中的行为设置,但我有行为没有经验。我还是用WPF有点湿



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



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



尝试了这一点斯坦:

 < Grid.Resources> 
<的DataTemplate X:键=WidgetTemplate>
< StackPanel的方向=横向>
<图像源=/资源/ gear1.png保证金=1=拉伸无/>
< TextBlock的文本={绑定名称}VerticalAlignment =中心保证金=6,0,0,0/>
< / StackPanel的>
< / DataTemplate中>

< HierarchicalDataTemplate X:键=NodeTemplate的ItemsSource ={结合儿童}的ItemTemplate ={StaticResource的WidgetTemplate}>
< TextBlock的文本={绑定名称}/>
< / HierarchicalDataTemplate>

< /Grid.Resources>


解决方案

这是你要去哪里想可能使用该附加的行为,你已经准备DoubleClick。



另外,这里是我使用它创建附加的行为,并会创建一个绑定到一个命令和可选的命令参数可以有两个附加属性的完整代码。



AttachedBehaviors.cs

 公共静态类MouseDoubleClick 
{
公共静态的DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached(命令,
typeof运算(ICommand的),
typeof运算(MouseDoubleClick),
新UIPropertyMetadata (CommandChanged));

公共静态的DependencyProperty CommandParameterProperty =
DependencyProperty.RegisterAttached(CommandParameter,
的typeof(对象),
typeof运算(MouseDoubleClick),
新UIPropertyMetadata(空值));

公共静态无效set命令(DependencyObject的目标,ICommand的值)
{
target.SetValue(CommandProperty,值);
}

公共静态无效SetCommandParameter(DependencyObject的目标,对象的值)
{
target.SetValue(CommandParameterProperty,值);
}
公共静态对象GetCommandParameter(DependencyObject的目标)
{
返回target.GetValue(CommandParameterProperty);
}

私有静态无效CommandChanged(DependencyObject的目标,DependencyPropertyChangedEventArgs E)
{
控制的控制=目标为控制;
如果(控制!= NULL)
{
如果((e.NewValue = NULL)及!及(e.OldValue == NULL))
{
control.MouseDoubleClick + = OnMouseDoubleClick;
}
,否则如果((e.NewValue == NULL)及及(e.OldValue = NULL)!)
{
control.MouseDoubleClick - = OnMouseDoubleClick;
}
}
}

私有静态无效OnMouseDoubleClick(对象发件人,RoutedEventArgs E)
{
控制的控制=发件人的控制;
ICommand的命令=(ICommand的)control.GetValue(CommandProperty);
对象commandParameter = control.GetValue(CommandParameterProperty);
如果(command.CanExecute(commandParameter))
command.Execute(commandParameter);
}
}



的.xaml - 记住要添加的,其中附加的行为在于该命名空间

 < Telerik的:RadTreeView IsExpandOnSingleClickEnabled =真
IsLineEnabled =真
保证金=5
的ItemsSource ={结合ItemsView}
的SelectedItem ={结合SelectedWidget,模式=双向}
的ItemTemplate ={StaticResource的NodeTemplate}
ACB:MouseDoubleClick.Command ={结合ShowItemCommand}/>



SampleViewModel.cs



 私人RelayCommand _showItemCommand; 
公共RelayCommand ShowItemCommand
{
得到
{
返回_showItemCommand? (_showItemCommand =
新RelayCommand(ShowItemDetails,IsItemSelected));
}
}


I've been working on this problem for a stupid amount of time. It is time to ask for directions despite my inner man saying "don't do it."

I am coding in WPF C# using MVVM design pattern. We try to adhere strictly to the pattern and put nothing in the code behind unless there is no option or it is completely unreasonable to do so. Having said that, I am working with a Telerik RadTreeView. Here is a snippet of it in my XAML:

<telerik:RadTreeView IsExpandOnSingleClickEnabled="True" IsLineEnabled="True" Margin="5" 
                                 ItemsSource="{Binding ItemsView}"
                                 SelectedItem="{Binding SelectedWidget, Mode=TwoWay}"
                                 ItemTemplate="{StaticResource NodeTemplate}" />

Currently the tree is working properly so that if you highlight a tree item and click the OK button on the view, all is good. However, I need to also allow the user to double click on one of the tree items. This means I already have a command and method, protected override void OkAction(), in my view model with the needed logic. Telerik supplies a property called ItemDoubleClick that is supposed to supply functionality for the tree item double click. But I can't find anything to allow me to do this in the view model. In other words, how do I do the binding? We also have a behavior setup in our project for double clicking that I was told I could use, but I have no experience with behaviors. I'm still a little wet with WPF.

If it helps, here is a link to the documentation for Telerik: http://www.telerik.com/help/wpf/radtreeview-events-overview.html

I would appreciate any help or direction anyone can provide.

Try this out Stan:

<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>

解决方案

This is where you are going to want to possibly use the Attached Behavior that you already have for the DoubleClick.

Otherwise, here is the complete code that I use which creates the Attached Behavior and will create two Attached Properties which bind to a Command and optionally a Command Parameter.

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);
    }
}

.xaml - Remember to add the namespace of where the Attached Behavior lies.

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

SampleViewModel.cs

private RelayCommand _showItemCommand;
public RelayCommand ShowItemCommand
{
    get
    {
        return _showItemCommand ?? (_showItemCommand =
            new RelayCommand(ShowItemDetails, IsItemSelected));
    }
}

这篇关于MVVM结合双击方法使用Telerik的radtreecontrol的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆