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