文本块文本中的简单XAML绑定

时间:2014-09-07 23:50:21

标签: c# xaml

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

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

现在,当页面启动时,在控制器中,有一个方法可以运行。

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
        }
    }

那为什么dosnt这个工作?我将Textblock的DataContext设置为当前类(它是MainPage分部类),属性是公共属性。

注意:今天是我第一次使用WinForms在.net 2.0框架上大量休息时使用.net 4.5和XAML。

3 个答案:

答案 0 :(得分:1)

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

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

2)使用标准事件签名进行名为APIinfotextChanged的事件,并在属性发生更改后引发该事件。

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

答案 1 :(得分:1)

您可能缺少必须使用RaiseProperyChange NotifyPropertyChage来更新绑定的部分。您的Model应该实现INotifyPropertyChanged接口

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

然后

RaisePropertyChanged(&#34; APIinfotext&#34);

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

答案 2 :(得分:1)

看起来你需要一个非常简单的例子来说明其他两个人在谈论什么。我们什么都不假。您需要正确设置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.";
    }
}

enter image description here

相关问题