MVVM绑定双击到使用telerik radtree控件的方法



我解决这个问题已经有很长时间了。是时候问路了,尽管我内心的男人说"不要这样做">

我使用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}" />

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

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

我将感谢任何人能提供的任何帮助或指导。

试试这个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>

这是您想要使用DoubleClick已经具有的附加行为的地方。

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

附加行为.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-记住添加附加行为所在的命名空间。

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

很明显,我没有Telerik代码,所以我无法提供我想要的帮助,但你可以尝试这样的东西。(免责声明:我是从头开始写的)

Grid.Resources中定义您的风格

<Style TargetType="{x:Type RadTreeViewItem }" x:Key="TreeViewItemStyle">
<EventSetter Event="MouseDoubleClick" Handler="{Binding DoubleClick}" />
</Style>

将样式添加到容器样式。

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

如果有效,请告诉我。

我尝试了几种方法来实现这一点。最后,我发现VS2012让我很恼火。我注意到在构建和运行中没有应用更改。

我打开VS2010发现我没有遇到同样的问题。在与我的老板交谈后,我们发现这是一个很好的例子,说明坚持MVVM可能不是最明智的选择,因为双击与UI严格相关。

我只是使用视图模型的实例化作为数据上下文,跳过后面的代码,进入视图模型。没花一秒钟就做到了。

至于其他解决方案,我相信这是完全可能的,但由于我的VS2012问题,我无法确认或否认我在这里发布的帖子。

最新更新