我ItemsControl
與DataTemplate
,看起來像這樣:如何在點擊項目時更改數據模板?
我想,當用戶點擊項目達到的效果 - 它垂直展開並顯示更多的信息。
我在想的唯一方法是將其更改爲ListBox
併爲選定的常規視圖創建2 DataTemplates
?
但我寧願註冊點擊Grid
並在我的虛擬機上翻轉屬性以展開此框。當用戶點擊Grid
MVVM方式時,有沒有辦法註冊?
我ItemsControl
與DataTemplate
,看起來像這樣:如何在點擊項目時更改數據模板?
我想,當用戶點擊項目達到的效果 - 它垂直展開並顯示更多的信息。
我在想的唯一方法是將其更改爲ListBox
併爲選定的常規視圖創建2 DataTemplates
?
但我寧願註冊點擊Grid
並在我的虛擬機上翻轉屬性以展開此框。當用戶點擊Grid
MVVM方式時,有沒有辦法註冊?
您可以使用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);
}
}
我不知道我是正確遵循這個問題,但它是否像你正在尋找的DataGrid RowDetails功能?它顯示了選擇項的詳細信息面板:
你希望能夠同時擴大多個項目?如果是這樣,用戶將如何摺疊擴展項目? ItemsControl不是選擇器,因此它不會註冊SelectedItem。你最好使用ListBox。 –