【问题标题】:Simple XAML binding in a textblock text文本块文本中的简单 XAML 绑定
【发布时间】:2014-09-07 23:50:20
【问题描述】:

我在 Windows 8.1 通用应用程序的 HubSection 中有一个 TextBlock 控件。

<TextBlock x:Name="api_enabled_label"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Text="{Binding APIinfotext}" />

现在当页面启动时,在contrustor中,有一个方法在运行。

public string APIinfotext { get; set; }    

public sealed partial class MainPage : Page {

    VoipMS voip_service = new VoipMS("shoukatali@hotmail.com", "Kitt0cat");

    public string APIinfotext { get; set; }

    public MainPage() {
        this.InitializeComponent();
        // disable sections until API is enabled
        mainpagehub.Sections[1].IsEnabled = false;
        mainpagehub.Sections[2].IsEnabled = false;
        //check for API and enable sections            
        checkAPI();
    }

    private async void checkAPI() {
        //irrelevant code above
        switch (result) {
            case "success":                        
                APIinfotext = "Your API is connected";                        
                break;
        //irrelevant code below
        }
    }

那么为什么不这样做呢?我将 Textblock 的 DataContext 设置为当前类(即 MainPage 部分类),并且该属性是公共属性。

注意:今天是我在 .net 2.0 框架与 WinForms 的巨大突破之后,第一次使用带有 XAML 的 .net 4.5。

【问题讨论】:

  • 缺少 INotifyPropertyChanged,查看解决方案

标签: c# xaml


【解决方案1】:

您的绑定不知道 APIinfotext 属性已更改。要让绑定知道属性已更改,您可以执行以下操作之一。第一个是最简单的。

1) 实现 INotifyPropertyChanged 接口,并在 APIinfotext 发生更改后引发 PropertyChanged 更改事件 (PropertyChanged("APIinfotext"));

2) 使用标准事件签名创建一个名为 APIinfotextChanged 的​​事件,并在属性更改后引发该事件。

3) 将您的属性实现为 DependencyProperty(在这种情况下不是理想的解决方案)。

【讨论】:

  • 所以我可以实现 INotifyPropertyChanges 到partial MainPage : Page 类?编辑:我这样做了,但我如何引发 PropertyChange 事件?
  • 您可以有一个从 Page 继承的基类,并在那里有您的 RaisePropertyChanged 并在您的主页中使用它。 SO public class Mainpage:Base {}
【解决方案2】:

您可能缺少必须 RaiseProperyChange NotifyPropertyChage 以更新绑定的部分。你的模型应该实现 INotifyPropertyChanged 接口

public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
  if (this.PropertyChanged != null)
    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

然后

RaisePropertyChanged("APIinfotext");

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.propertychanged.aspx

【讨论】:

  • 这不起作用。我错过了什么吗?我在上面复制了你的代码。并进行了以下更改public sealed partial class MainPage : Page, INotifyPropertyChanged {
  • 您是否进行了以下更改? case "success": APIinfotext = "您的 API 已连接"; RaisePropertyChanged("APIinfotext");
  • 是的,但我错过了 datacontext 值。数据上下文设置为空。
【解决方案3】:

看起来你需要一个非常简单的例子来说明其他两个人在谈论什么。让我们假设什么。您需要正确设置 DataContext,并引发事件。这很简单,当您单击按钮时,它将更改 TextBox,因为我更改了引发事件的属性。


XAML

<Page>
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel>
            <TextBox Text="{Binding APIinfotext}"  Height="100" Width="400" HorizontalAlignment="Left"/>
            <Button x:Name="myButton" Content="Change Text" Height="200" Width="400" Click="myButton_Click"/>
        </StackPanel>
    </Grid>
</Page>

C#(注意,APIinfotext的SET部分)

using System.ComponentModel;   // INotifyPropertyChanged

public sealed partial class MainPage : Page, INotifyPropertyChanged
{

    private string _apiinfotext = "Default Text";
    public string APIinfotext 
    {
        get { return _apiinfotext; }
        set
        {
            _apiinfotext = value;
            RaisePropertyChanged("APIinfotext");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public MainPage()
    {
        this.InitializeComponent();

        this.DataContext = this;
    }

    private void myButton_Click(object sender, RoutedEventArgs e)
    {
        this.APIinfotext = "Don't confuse movement for progress.";
    }
}

【讨论】:

  • 谢谢!我错过了 DataContext。在阅读了更多关于它的信息后,我看到它是通过所有子属性继承的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-28
  • 2016-01-17
相关资源
最近更新 更多