1

我看起来像这样ItemsControlDataTemplate

在此处输入图像描述

我想在用户单击项目时达到效果 - 它垂直扩展并显示更多信息。

我在想的唯一方法是将其更改为并为选定视图和常规视图ListBox创建 2 ?DataTemplates

但我宁愿Grid在我的虚拟机上注册单击并翻转属性以展开此框。Grid当用户点击MVVM 方式时,有什么方法可以注册?

4

2 回答 2

1

MouseDown您可以为事件使用附加行为。
请参阅以下问题:WPF/MVVM - 如何处理 ViewModel 中的 TreeViewItems 双击?

在你的情况下,它看起来像这样

<ItemsControl ItemsSource="{Binding ...}">
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="commandBehaviors:MouseDown.Command"
                    Value="{Binding YourItemClickCommand}"/>
            <Setter Property="commandBehaviors:MouseDown.CommandParameter"
                    Value="{Binding}"/>
        </Style>
    </ItemsControl.ItemContainerStyle>
    <!-- ... -->
</ItemsControl>

鼠标按下

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

    public static DependencyProperty CommandParameterProperty =
        DependencyProperty.RegisterAttached("CommandParameter",
                                            typeof(object),
                                            typeof(MouseDown),
                                            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.MouseDown += OnMouseDown;
            }
            else if ((e.NewValue == null) && (e.OldValue != null))
            {
                control.MouseDown -= OnMouseDown;
            }
        }
    }

    private static void OnMouseDown(object sender, RoutedEventArgs e)
    {
        Control control = sender as Control;
        ICommand command = (ICommand)control.GetValue(CommandProperty);
        object commandParameter = control.GetValue(CommandParameterProperty);
        command.Execute(commandParameter);
    }
}
于 2012-06-19T20:42:44.420 回答
0

我不确定我是否正确地关注了这个问题,但它是否类似于您正在寻找的 DataGrid RowDetails 功能?它显示所选项目的详细信息面板:

http://wpftutorial.net/DataGrid.html#rowDetails

于 2012-06-19T22:19:09.707 回答