1

我有一个文本框和一个按钮。按钮命令应该更改绑定到文本框的属性。

但是我在命令执行后看不到任何视觉变化。我认为与 wpf 绑定有关的简单问题

请帮我解决这个问题

应用程序来源:

<UserControl.DataContext>
    <local:SampleViewModel />
</UserControl.DataContext>
<Grid>
    <StackPanel>
        <TextBox Height="23" Width="120" Text="{Binding MyName}"  />
        <Button Content="Click" Command="{Binding ButtonCommand}" />
    </StackPanel>
</Grid>

视图模型:

Private _myName As String
Public Property MyName As String
  Get
    Return _myName
  End Get
  Set(value As String)
    _myName = value
      OnPropertyChanged("MyName")
  End Set
End Property

Public _buttonCommand As DelegateCommand
Public ReadOnly Property ButtonCommand As DelegateCommand
Get
  Return If(_buttonCommand IsNot Nothing, _buttonCommand, 
  New DelegateCommand(AddressOf Execute, AddressOf CanExecute))
End Get
End Property

Private Sub Execute()
  MyName = "Executed"
End Sub

Private Function CanExecute() As Boolean
  Return True
End Function

Public Event PropertyChanged As PropertyChangedEventHandler
Private Sub OnPropertyChanged(propertyName As String)
  RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
4

2 回答 2

1

这是一个适用于您的确切 XAML 的代码(我从http://wpftutorial.net/DelegateCommand.html 获取了 DelegateCommand 实现)对不起,它是 C#,我真的不喜欢 VB:D

public class DelegateCommand : ICommand
{
    private readonly Predicate<object> _canExecute;
    private readonly Action<object> _execute;

    public event EventHandler CanExecuteChanged;

    public DelegateCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    public DelegateCommand(Action<object> execute,
                   Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    public virtual bool CanExecute(object parameter)
    {
        if (_canExecute == null)
        {
            return true;
        }

        return _canExecute(parameter);
    }

    public virtual void Execute(object parameter)
    {
        _execute(parameter);
    }

    public void RaiseCanExecuteChanged()
    {
        if (CanExecuteChanged != null)
        {
            CanExecuteChanged(this, EventArgs.Empty);
        }
    }
}

public class SampleViewModel : INotifyPropertyChanged
{
    // Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    public DelegateCommand _buttonCommand;
    public DelegateCommand ButtonCommand
    {
        get
        {
            if (_buttonCommand == null)
            {
                _buttonCommand = new DelegateCommand(Execute);
            }
            return _buttonCommand;
        }
    }

    public void Execute(object o)
    {
        MyName = "executed";
    }

    public string MyName { get { return _myName; } set { _myName = value; OnPropertyChanged("MyName"); } }

    private string _myName;
}
于 2012-10-16T15:32:52.183 回答
1

请执行下列操作:

1.MainWindow类实现INotifyPropertyChanged

2. 在 Public Sub New() 中确保你写Me.DataContext = Me来设置 DataContext

注意:如果您使用 ViewModel 并在 XAML 中设置,请忽略第 2 步

3. 像这样修改 ProperyChanged: Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

只有正确实现INotifyPropertyChanged后,Binding 才会在 PropertyChanged 事件之后正确刷新 MyName 属性

于 2012-10-16T15:29:56.153 回答