Hiçbir yerleşik bir yolu, işte olduğu ben bunu nasıl: Biz EventArgs
MouseEnter
ait erişemez
<Button Content="I am a button">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<i:InvokeCommandAction Command="{Binding CommandWithNoArgs}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
: Klasik Etkileşim tetik böyle kullanılır
Bağlanma yoluyla olay, bu yüzden onu dışarı atıyor parça değiştirmek zorunda kalacağız. O sırada da
, o yazı InvokeCommandAction
olduğunu.
"Yani sadece alt sınıf ve birlikte hepimiz için bekliyordu uygun bir yöntemi geçersiz gidiyoruz" Ben yazmayı isterdim budur. Ama sınıf mühürlendi.
yüzden onun üst alt sınıfı için zorunda gidiyoruz (soyut) sınıfı:
TriggerAction<DependencyObject>
en temel uygulamasıdır:
public class InteractiveCommand : TriggerAction<DependencyObject>
{
protected override void Invoke(object parameter)
{
}
}
Ve bu parameter
senin EventArgs
olduğunu!
Ama durun, o kadar basit değil, düzenli InvokeCommandAction
davranışını çoğaltmak gerekir. Reflektör sayesinde
, bunu bozulamaz (ama, ben sadece tembelim resmi kaynağına bakmak gidebiliriz).
CommandParameter
bağımlılık özelliği hakkında umrumda değil, biz bunu InvokeCommandAction
yerine kullanırsanız, aslında her zaman EventArgs
olmasını istersiniz. İşte
(WPF sadece SilverLight için EDIT bakınız) tam sınıfını geçer:
internet, alımın herhangi bir kod olduğu gibi
public class InteractiveCommand : TriggerAction<DependencyObject>
{
protected override void Invoke(object parameter)
{
if (base.AssociatedObject != null)
{
ICommand command = this.ResolveCommand();
if ((command != null) && command.CanExecute(parameter))
{
command.Execute(parameter);
}
}
}
private ICommand ResolveCommand()
{
ICommand command = null;
if (this.Command != null)
{
return this.Command;
}
if (base.AssociatedObject != null)
{
foreach (PropertyInfo info in base.AssociatedObject.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (typeof(ICommand).IsAssignableFrom(info.PropertyType) && string.Equals(info.Name, this.CommandName, StringComparison.Ordinal))
{
command = (ICommand)info.GetValue(base.AssociatedObject, null);
}
}
}
return command;
}
private string commandName;
public string CommandName
{
get
{
base.ReadPreamble();
return this.commandName;
}
set
{
if (this.CommandName != value)
{
base.WritePreamble();
this.commandName = value;
base.WritePostscript();
}
}
}
#region Command
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
// Using a DependencyProperty as the backing store for Command. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(InteractiveCommand), new UIPropertyMetadata(null));
#endregion
}
ederim tüm sınıfa aracılığıyla okuma ve anlamaya çalışmak salık ne yapar. Sadece uygulamanıza atmayın.Şimdi
Yapabileceğimiz:
<Button Content="I am a button">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<local:InteractiveCommand Command="{Binding CommandWithEventArgs}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
Ve arkasındaki kodu:
#region CommandWithEventArgs
DelegateCommand<MouseEventArgs> _CommandWithEventArgs;
/// <summary>
/// Exposes <see cref="CommandWithEventArgs(MouseEventArgs)"/>.
/// </summary>
public DelegateCommand<MouseEventArgs> CommandWithEventArgs
{
get { return _CommandWithEventArgs ?? (_CommandWithEventArgs = new DelegateCommand<MouseEventArgs>(CommandWithEventArgs)); }
}
#endregion
public void CommandWithEventArgs(MouseEventArgs param)
{
}
Ve bu Çekim bitti;)
DÜZENLEME: SilverLight için, bu kodu yerine kullanın:
public class InteractiveCommand : TriggerAction<DependencyObject>
{
protected override void Invoke(object parameter)
{
if (base.AssociatedObject != null)
{
ICommand command = Command;
if ((command != null) && command.CanExecute(parameter))
{
command.Execute(parameter);
}
}
}
#region Command
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
// Using a DependencyProperty as the backing store for Command. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(InteractiveCommand), new UIPropertyMetadata(null));
#endregion
}
Ancak, lütfen tam teşekküllü WPF versiyonunu kullanmanın daha az güvenli olduğunu unutmayın (türleri kontrol etmiyor, donmuş elemanlarla çökebilir).
Sorunun başlığına etiket koymayın. İyi başlıklar yazarken [bu bağlantıyı] tıklayın (http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title);) –