在我的 C# XAML Windows 8.1 应用程序中,如何使编程绑定工作?

In my C# XAML Windows 8.1 app, how do I make programmatic binding work?

我是 Windows 8.1 开发以及 C# 和 XAML 的新手,如果这是一个非常基本的问题,请原谅我。首先,我使用以下页面作为我在这里所做的大部分工作的灵感来源:

https://msdn.microsoft.com/en-us/library/ms742863(v=vs.110).aspx

这是我页面的XAML:

<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Button HorizontalAlignment="Left" Content="this is my button"  Click="Button_Click" />
    <TextBox Text="This is a bound text box" x:Name="BoundTextBox" />
    <TextBlock Text="This is a bound text block" x:Name="BoundTextBlock" />
</StackPanel>

这是我的隐藏代码:

public sealed partial class MyPage : Page, INotifyPropertyChanged
    {

        #region Str variable
        // Create the source string.
        private string str = "Initial Value of the 'str' variable";

        public string Str
        {
            get { return str; }
            set { 
                str = value;
                NotifyPropertyChanged("Str");
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }       
        #endregion

        ...

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Create the binding description.
            Binding binding = new Binding(); // 'Binding' has no one-parameter constructors, so changed from MS's documentation
            binding.Mode = BindingMode.OneWay; // I tried TwoWay, as well, but that also did not work
            binding.Source = Str; 

            // Attach the binding to the target.
            BoundTextBox.SetBinding(TextBox.TextProperty, binding);
            BoundTextBlock.SetBinding(TextBlock.TextProperty, binding);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.TextColorBrush = new SolidColorBrush(Colors.Red);

            if (Str == "Changed Value ONE")
            {
                Str = "SECOND Changed Value";
            }
            else
            {
                Str = "Changed Value ONE";
            }
        }
    }

发生的事情是 TextBox 和 TextBlock 都将它们的文本 属性 设置为 Initial Value of the 'str' variable,但是当我单击按钮时 str 的基础值发生变化(我已验证实际上是通过断点和步进发生的),TextBlock 和 TextBox 的文本值不会改变。

当我以另一种方式进行绑定时(即在 XAML 而不是在代码隐藏中),它 确实 起作用。这是我为实现这一目标所做的一切:

    <Page
        x:Name="This"
        ...
    >
        <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Button HorizontalAlignment="Left" Content="this is my button"  Click="Button_Click" />
            <TextBox Text="{Binding ElementName=This, Path=Str, Mode=OneWay}" x:Name="BoundTextBox" />
            <TextBlock Text="{Binding ElementName=This, Path=Str, Mode=OneWay}" x:Name="BoundTextBlock" />
        </StackPanel>
    </Page>

所以我的主要问题是 "How can I get the code-behind data binding to work, like the XAML-based binding works?"。

提前感谢您提供的任何帮助、建议或提示!

你应该设置 binding.Source = this; binding.Path = new PropertyPath("Str")

原因是如果你设置 binding.Source = Str ,它只会获取 Str 的值,并且无法检测 change.Therefore 源应该是 class 实现 INotifyPropertyChanged 并且还给出它是一个路径,以便绑定 class 观察 属性

的变化