【问题标题】:Can not bind textblock property from another class to UI class using MVVM无法使用 MVVM 将另一个类的文本块属性绑定到 UI 类
【发布时间】:2011-10-07 00:12:18
【问题描述】:

我是 sliverlight 和 MVVM 的初学者。 我无法使用 MVVM 将另一个类的文本块属性绑定到 UI 类。

我的代码在这里。 请让我知道如何在下面的 Authentication.cs 中绑定 textblock 属性。

MainPage.xaml

<TextBlock Height="30" Margin="122,218,0,0" Name="textBlock3" Text="{Binding Path=ErrorStatus, Mode=TwoWay}" VerticalAlignment="Top" HorizontalAlignment="Left" Width="86" />

MainPage.xaml.cs

private Authentication authentication;

// Constructor
public MainPage()
{
    InitializeComponent();
    this.DataContext = authentication;
}

ViewModelBase.cs

public class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

身份验证.cs

public class Authentication : ViewModelBase
{
    private string _ErrorStatus;
    public string ErrorStatus
    {
        get
        {
            return _ErrorStatus;
        }
        set
        {
            _ErrorStatus = value;
            NotifyPropertyChanged("ErrorStatus");
        }
    }

    void Authenticate()
    {
        //Here, I write authentication code....
        //After the authentication code, I want to change textBlock property depend on auth status.
        //Please let me know how to write code.
    }
}

【问题讨论】:

    标签: silverlight windows-phone-7 mvvm


    【解决方案1】:

    你在你的Authenitcate()方法中写ErrorStatus = "Access Denied";

    TextBox 的 Text 指向 ErrorStatus 属性,所以只要它更新,TextBox 也会自动更新。

    您唯一需要确定的是,您在 TextBox 绑定到的同一个对象上调用了Authenticate()

    private Authentication authentication = new Authentication();
    
    public MainPage()
    {
        InitializeComponent();
    
        // New line here
        this.authentication = new Authentication();
    
        this.DataContext = authentication;
    }
    
    void btnAuthenticate_Click(object src, EventArgs e)
    {
        authentication.Authenticate();
    }
    

    XAML:

    <TextBlock Text="{Binding Path=ErrorStatus, Mode=TwoWay}" />
    

    【讨论】:

      【解决方案2】:

      您尚未创建Authentication 的新实例。将以下行添加到主窗口构造函数:

      // Constructor
      public MainPage()
      {
          InitializeComponent();
      
          // New line here
          this.authentication = new Authentication();
      
          this.DataContext = authentication;
      }
      

      当您调用Authenticate() 时,您只需为ErrorStatus 分配一个新值,它应该会显示在TextBlock 中。

      【讨论】:

      • 我在 MainPage.xaml 的 Button(button1_click) 中调用 Authentication() 实例。因此,如果我添加了 this.authentication = new Authentication();,我将无法更改属性。 void Authenticate() { //这里,我写验证码.... //验证码之后,我想根据验证状态改变textBlock属性。 //请让我知道如何编写代码。 //我添加了下面的代码。但我不能改变财产。 ErrorStatus = "访问被拒绝。" }
      • 为什么不呢?一旦正确设置了 DataContext(在构造函数中,在按钮单击代码之前),更新视图模型将导致绑定将值推送到 TextBlock
      • @okame 您想在构造函数中添加该行,而不是在 Authenticate 中。在构造函数中添加该行后,您无需对 Authenticate 进行任何更改(除了为 ErrorStatus 分配新值)
      • 谢谢回复。我想在 Authentication 类中为 ErroStatus 分配一个新值。所以我添加了代码 ErrorStatus = "Access Denied.";在验证()中。但是当调试器检查代码时,ErrorStatus 没有改变。请让我知道如何更改 Authenticate() 中的 ErrorStatus。
      【解决方案3】:

      这就是委托命令模式(也称为中继命令)发挥作用的地方http://kharasoft.wordpress.com/2007/10/16/the-delegating-command/ 使用此模式,而不是在您的代码中处理按钮单击,您的视图模型公开了一个 ICommand 实例,该实例仅用于路由事件到视图模型上的方法。现在身份验证在您的视图模型的上下文中运行,并且可以直接更新属性。

      【讨论】:

      • Laurent Bugnion 在他的 mvvmlight 库中提供了一个委托命令的实现,该库在 silverlight 中工作
      猜你喜欢
      • 1970-01-01
      • 2012-07-11
      • 2015-06-15
      • 2016-07-06
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      • 2015-10-22
      相关资源
      最近更新 更多