2012-06-19 2 views
1

나는 ItemsControl 다음과 같습니다 그 DataTemplate에 있습니다항목을 클릭 할 때 데이터 템플릿을 변경하는 방법은 무엇입니까?

enter image description here

나는 사용자가 항목을 클릭 할 때 효과를 달성하고자하는 - 그것은 수직으로 확장하고 더 많은 정보를 보여줍니다.

제가 생각하는 유일한 방법은 ListBox으로 변경하고 선택하고 정기적으로 볼 때 2 DataTemplates을 만드시겠습니까?

하지만이 상자를 확장하려면 Grid을 클릭하고 내 VM의 속성을 뒤집어서 등록하십시오. 사용자가 Grid MVVM 방식으로 클릭 할 때 등록 할 방법이 있습니까?

+0

당신이 한 번에 여러 항목을 확장 할 수 하시겠습니까 :이 선택 항목에 대한 세부 사항 패널을 보여줍니다? 그렇다면 사용자가 확장 된 항목을 어떻게 축소할까요? ItemsControl은 Selector가 아니므로 SelectedItem을 등록하지 않습니다. ListBox를 사용하는 것이 가장 좋습니다. –

답변

1

MouseDown 이벤트에 대해 연결된 동작을 사용할 수 있습니다.
가 다음과 같은 질문을 참조하십시오 : 귀하의 경우 WPF/MVVM - how to handle double-click on TreeViewItems in the ViewModel?

는 내가 질문을 제대로 다음있어 확실하지 않다이

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

MouseDown

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); 
    } 
} 
관련 문제