2013-11-05 35 views
0

我在视图上有10个按钮,点击时应该将ViewModel的适当属性设置为null(ViewModel是View的DataContext)。把它看作是重置行动。WPF按钮:在xaml的Click事件中将属性设置为null

目前我所拥有的是ViewModel中的ICommand,每个按钮的Command都绑定了,然后我将CommandParameter设置为某个值,这将允许我区分ViewModel上哪些属性需要更新。

为了避免一堆的IFS,我想到做这样的事情(不正确的语法)的:

<Button ...> 
    <i:Interaction.Triggers> 
    <i:EventTrigger EventName="Click"> 
     <Setter Property="PropertyA" Source="DataContext-ViewModel" Value="x:null" /> 
    </i:EventTrigger> 
    <i:Interaction.Triggers> 
</Button> 

这是可能实现的,以及如何

+0

而不是使用'if'为什么不使用'switch'。不知道你是否有可能要求。 – Sandesh

+0

@ aks81我没有看到你回答的方式与我的问题有关吗?你建议使用另一个控制语句,而我试图避免使用它。 – Goran

+0

使用反射不是您的选择吗? – sthotakura

回答

0

如果使用Reflection一个选项然后是你 您可以使用Reflection在ViewModel中设置属性。

你的命令的执行方法将成为这样的事情:

this.GetType().GetProperty((string)CommandParameter).SetValue(this, null, new object[] {}); 

但是,如果你愿意,你在你的问题中提到的XAML路线,那么你可以创建TriggerAction和使用,在EventTrigger。下面是我的尝试:

public sealed class SetProperty : TriggerAction<FrameworkElement> 
{ 
    public static readonly DependencyProperty SourceProperty = 
     DependencyProperty.Register("Source", typeof (object), typeof (SetProperty), new PropertyMetadata(default(object))); 

    /// <summary> 
    /// Source is DataContext 
    /// </summary> 
    public object Source 
    { 
     get { return (object) GetValue(SourceProperty); } 
     set { SetValue(SourceProperty, value); } 
    } 

    public static readonly DependencyProperty PropertyNameProperty = 
     DependencyProperty.Register("PropertyName", typeof (string), typeof (SetProperty), new PropertyMetadata(default(string))); 

    /// <summary> 
    /// Name of the Property 
    /// </summary> 
    public string PropertyName 
    { 
     get { return (string) GetValue(PropertyNameProperty); } 
     set { SetValue(PropertyNameProperty, value); } 
    } 

    public static readonly DependencyProperty ValueProperty = 
     DependencyProperty.Register("Value", typeof (object), typeof (SetProperty), new PropertyMetadata(default(object))); 

    /// <summary> 
    /// Value to Set 
    /// </summary> 
    public object Value 
    { 
     get { return (object) GetValue(ValueProperty); } 
     set { SetValue(ValueProperty, value); } 
    } 

    protected override void Invoke(object parameter) 
    { 
     if (Source == null) return; 
     if (string.IsNullOrEmpty(PropertyName)) return; 

     Source.GetType().GetProperty(PropertyName).SetValue(Source, Value, new object[] {}); 
    } 
} 

和XAML:

<Button Content="Reset"> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="Click"> 
      <local:SetProperty Source="{Binding}" PropertyName="PropertyToSet" Value="{x:Null}" /> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
</Button> 

如果你不喜欢思考的话,那么你可以在你的视图模型操作的字典:

_propertyResetters = new Dictionary<string, Action> 
{ 
    {"PropertyToSet",() => PropertyToSet = null} 
}; 

而且,在你的Command Execute方法中,你可以通过做_propertyResetters[(string)CommandParameter]();

希望这可以帮助或给你一些想法。

+0

嗨sthotakura,谢谢你的例子。我不知道你可以定义一个自定义的TriggerAction,所以这个信息很有价值。 – Goran

+0

这可以修改或添加到,以便您可以指定一个不同的控件的属性来定位?像TargetName一样? – jrandomuser

+0

@jrandomuser不知道我是否理解你的问题。你能再详细一点吗? – sthotakura

相关问题