3

Just started working with the MVVM design pattern and I'm stuck.

When my application launches, I have a treeview populated with a list of objects names. I've setup the IsChecked Binding, and it works fine. I'm trying to setup the IsEnabled Binding.

I want the user to select the items in the treeview he wants, then click one of three buttons to perform an action. On click, I want the selected items to remain in the treeview, but be disabled, so the user cannot perform another action on those items.

I'm using a RelayCommand class in the application.

private ICommandOnExecute _execute;
private ICommandOnCanExecute _canExecute;

public RelayCommand(ICommandOnExecute onExecuteMethod, 
    ICommandOnCanExecute onCanExecuteMethod)
{
    _execute = onExecuteMethod;
    _canExecute = onCanExecuteMethod;
}

#region ICommand Members

public event EventHandler CanExecuteChanged
{
    add { CommandManager.RequerySuggested += value; }
    remove { CommandManager.RequerySuggested -= value; }
}

public bool CanExecute(object parameter)
{
    return _canExecute.Invoke(parameter);
}

public void Execute(object parameter)
{
    _execute.Invoke(parameter);
}

#endregion

My object model class uses this

private bool _isEnabled;
public bool IsEnabled
{
    get { return true; }
    set { _isEnabled = value};
}

Then within my button method I have

if (interfaceModel.IsChecked)
{
    //Does Something
    MyObjectName.IsEnabled = false;
}

And here is my xaml

<CheckBox IsChecked="{Binding IsChecked}" IsEnabled="{Binding IsEnabled, Mode=TwoWay}">
    <TextBlock Text="{Binding MyObjectName}" Margin="5,2,1,2" HorizontalAlignment="Left" />
</CheckBox>
4

1 回答 1

2

您需要这样的设置:

// Your ViewModel should implement INotifyPropertyChanged
class ViewModel : INotifyPropertyChnaged
{
    private bool _isEnabled;
    public bool IsEnabled
    {
        get { return _isEnabled; }
        set 
        { 
             _isEnabled = value;
             SetPropertyChanged("IsEnabled");  // Add this to your setter.
        }
    }

    // This comes from INotifyPropertyChanged - the UI will listen to this event.
    public event PropertyChangedEventHandler PropertyChanged;
    private void SetPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged( this, new PropertyChangedEventArgs(property) );
        }
    }
}

请注意,这PropertyChanged来自让您的 ViewModel 实现INotifyPropertyChanged。要通知 UI,您必须引发该事件,并告诉它更改了哪些属性(通常在 setter 中 - 见上文)。

或者,如果您不喜欢原始字符串(我个人不喜欢),您可以使用泛型和表达式树来执行以下操作:

public void SetPropertyChanged<T>(Expression<Func<T, Object>> onProperty) 
{
    if (PropertyChanged != null && onProperty.Body is MemberExpression) 
    {
        String propertyNameAsString = ((MemberExpression)onProperty.Body).Member.Name;
        PropertyChanged(this, new PropertyChangedEventArgs(propertyNameAsString));
    }
}

在您的二传手中,您可以说:

public bool IsEnabled
{    
    set 
    { 
        _isEnabled = value;
        SetPropertyChanged<ViewModel>(x => x.IsEnabled);  
    }
}

现在它是强类型的,这有点好。

于 2013-10-07T16:03:53.623 回答