사용자 지정 컨트롤 인 "GameIconControl"의 인스턴스가 여러 개있는 랩 패널이 있습니다. 내 목표는 사용자 지정 컨트롤을 두 번 클릭하면 랩 패널에서 제거되도록하는 것입니다.사용자 지정 컨트롤을 두 번 클릭 이벤트를 MainViewModel 명령에 바인딩
랩 패널은 ObservableCollection에 바인딩됩니다.
다음은 사용자 지정 컨트롤 xaml입니다. 더블 클릭 명령 바인딩을 추가하려고 시도했지만 랩 패널에로드 된 명령을 찾는 방법을 알지 못합니다. 실험으로, MainViewModel이 각 GameIconControl을로드 할 때 MainViewModel이 명령이있는 곳이기 때문에 "각각"에 대한 DataContext를 설정하려고했습니다. 불운.
<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 Lite를 사용하고 있습니다.
<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