我有一个由我的自定义控件“GameIconControl”的多个实例填充的包装面板。我的目标是在双击自定义控件时让它们从包装面板中删除。将自定义控件双击事件绑定到MainViewModel命令
包装面板绑定到一个ObservableCollection。
下面是自定义控件xaml。我试图添加双击命令绑定,但它不知道如何在包装面板中加载该命令后才能找到该命令。作为一个实验,当MainViewModel加载每个GameIconControl时,我尝试将DataContext设置为“this”,因为MainViewModel是该命令所在的位置。没有运气。
<Style TargetType="{x:Type assets:GameIconControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type assets:GameIconControl}">
<Border Margin="3"
Background="Transparent"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel HorizontalAlignment="Center"
VerticalAlignment="Center"
Orientation="Vertical">
<Image Source="{TemplateBinding Icon}" />
<ContentPresenter Height="Auto"
Margin="3"
HorizontalAlignment="Center"
ContentSource="GameName" />
</StackPanel>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<command:EventToCommand Command="{Binding MoveGameIconToGamesList, Mode=OneWay}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
该控件背后的代码很简单。三个DP和构造函数:
static GameIconControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(GameIconControl), new FrameworkPropertyMetadata(typeof(GameIconControl)));
}
public GameIconControl(){}
public GameIconControl(string name, BitmapImage icon)
{
Icon = icon;
GameName = name;
}
public GameIconControl(string name, BitmapImage icon, Game game) {
GameForControl = game;
Icon = icon;
GameName = name;
}
public const string IconPropertyName = "Icon";
public const string GameNamePropertyName = "GameName";
public const string GamePropertyName = "Game";
public string GameName
{
get { return (string)GetValue(GameNameProperty); }
set { SetValue(GameNameProperty, value); }
}
public static readonly DependencyProperty GameNameProperty =
DependencyProperty.Register(GameNamePropertyName, typeof(string), typeof(GameIconControl), new UIPropertyMetadata(default(string)));
public BitmapImage Icon
{
get { return (BitmapImage)GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
}
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register(IconPropertyName, typeof(BitmapImage), typeof(GameIconControl), new PropertyMetadata(default(BitmapImage)));
public Game GameForControl
{
get { return (Game)GetValue(GameProperty); }
set { SetValue(GameProperty, value); }
}
public static readonly DependencyProperty GameProperty =
DependencyProperty.Register(GamePropertyName, typeof(Game), typeof(GameIconControl), new PropertyMetadata(default(Game)));
}
任何帮助,非常感谢。
最后,这里是开放的xaml。我正在使用MVVM精简版:
<Window x:Class="Saved_Game_Backup.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cc="clr-namespace:Saved_Game_Backup.Assets"
xmlns:command="http://www.galasoft.ch/mvvmlight"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:viewModel="clr-namespace:Saved_Game_Backup.ViewModel"
DataContext="{Binding Path=Main,
Source={StaticResource Locator}}">
尝试使用'的RelativeSource FindAncestor'结合如图[这里](http://stackoverflow.com/a/1127964/2998271)。这使您可以绑定到控件的父级DataContext,但没有在你的情况下从样式中尝试它。 – har07