代码之家  ›  专栏  ›  技术社区  ›  ebattulga

如何在WPF中绑定局部变量

  •  0
  • ebattulga  · 技术社区  · 14 年前

    我有Silverlight用户控件。这包含服务实体对象。见下文

    public partial class MainPage : UserControl
    {
       public ServiceRef.tPage CurrentPage { get; set; }
    ...
    }
    

    我需要捆绑 CurrentPage.Title 到文本框

    我的XAML在这里

    <TextBox Text="{Binding Path=CurrentPage.Title, RelativeSource={RelativeSource self}}"></TextBox>
    

    但这不是工作。

    如何做到这一点?

    3 回复  |  直到 14 年前
        1
  •  2
  •   Adam Robinson    14 年前

    为了使其发挥作用,您必须实现 INotifyPropertyChanged 在你的班上提高 PropertyChanged 事件为 CurrentPage 设置后(这也意味着您将无法使用自动属性;您必须使用自己的私有实例支持变量并对 get { } set { } 你自己)。

    发生的是控件在设置之前绑定到值 当前页码 . 因为您没有通知任何人属性已更改,所以不知道刷新绑定数据。实施 InotifyProperty已更改 会解决这个问题。

    或者您可以手动设置 Text 在setter中设置您自己的属性。

        2
  •  0
  •   decyclone    14 年前

    将标记更改为

    <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=CurrentPage.Title}" />
    

    通过分配 RelativeSource={RelativeSource self} 你在告诉 TextBlock 绑定到自身并查找名为 CurrentPage 控件 而不是父母 Window .

        3
  •  0
  •   Prince Ashitaka    14 年前

    设置 UpdateSourceTrigger="PropertyChanged" 在XAML中。