【问题标题】:WPF Binding to local variableWPF绑定到局部变量
【发布时间】:2010-12-01 15:42:51
【问题描述】:

你能像这样绑定到局部变量吗?

SystemDataBase.cs

namespace WebWalker
{
    public partial class SystemDataBase : Window
    {
        private string text = "testing";
...

SystemDataBase.xaml

 <TextBox 
       Name="stbSQLConnectionString" 
       Text="{SystemDataBase.text}">
 </TextBox>

??

文本设置为局部变量“文本”

【问题讨论】:

    标签: wpf data-binding binding


    【解决方案1】:

    模式是:

    public string Text {get;set;}
    

    绑定是

    {Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}
    

    如果您希望绑定自动更新,您应该将其设为 DependencyProperty。


    我认为 3.5 将 ElementName 添加到绑定中,因此以下内容更容易一些:

    <Window x:Name="Derp" ...
      <TextBlock Text="{Binding Text, ElementName=Derp}"/>
    

    【讨论】:

    • 或者让类实现 INotifyPropertyChanged 以使绑定自动工作。
    • 我认为您的意思是将其更改为 DependencyProperty。在 DependencyObject 上实现 INPC 将是愚蠢的。
    • 想一想,在这种情况下 ElementName 是什么?窗口的名称?
    • @killianmcc:就在代码示例中。 &lt;Window Name="Derp" Derp 是一个(至少我通常是这样做的)在你的窗口类上定义的 DependencyProperty (public partial class Herp : Window {})
    【解决方案2】:

    要绑定到本地“变量”,变量应该是:

    1. 属性,而不是字段。
    2. 公开。
    3. 通知属性(适用于模型类)或依赖属性(适用于视图类)

    通知属性示例:

    public MyClass : INotifyPropertyChanged
    {
        private void PropertyType myField;
    
        public PropertyType MyProperty
        {
            get
            {
                return this.myField;
            }
            set
            {
                if (value != this.myField)
                {
                    this.myField = value;
                    NotifyPropertyChanged("MyProperty");
                }
            }
        }
    
        protected void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    }
    

    依赖属性示例:

    public MyClass : DependencyObject
    {
        public PropertyType MyProperty
        {
            get
            {
                return (PropertyType)GetValue("MyProperty");
            }
            set
            {
                SetValue("MyProperty", value);
            }
        }
    
        // Look up DependencyProperty in MSDN for details
        public static DependencyProperty MyPropertyProperty = DependencyProperty.Register( ... );
    }
    

    【讨论】:

      【解决方案3】:

      如果您经常这样做,您可以考虑将整个窗口的 DataContext 绑定到您的类。这将默认被继承,但仍然可以像往常一样被覆盖

      <Window DataContext="{Binding RelativeSource={RelativeSource Self}}">
      

      那么对于你可以使用的单个组件

      Text="{Binding Text}"
      

      【讨论】:

      • 我知道这个问题是针对 WPF 的,但是如果您的目标是 Silverlight,那么这是要走的路。由于 Silverlight for Web 和 Phone 没有针对 RelativeSource 的 FindAncestor 方法
      【解决方案4】:

      要绑定 Window 类中存在的局部变量,它必须是: 1. 公共财产 2. 通知财产。为此,您的窗口类应为此属性实现 INotifyPropertyChanged 接口。

      然后在构造函数中

      public Assgn5()
      {           
          InitializeComponent();
      
          this.DataContext = this; // or **stbSQLConnectionString**.DataContext = this;
      }
      
       <TextBox 
         Name="stbSQLConnectionString" 
         Text="{Binding text}">
       </TextBox>
      

      【讨论】:

        【解决方案5】:

        需要在 int 构造函数中添加如下一行:

        this.DataContext = this;
        

        并在 XAML 中使用它:

        <TextBox Text = "{Binding SomeProperty}" />
        

        示例:

        MainWindow.xaml.cs

        public partial class MainWindow : Window
        {
            public string PersonName { get; set; }
        
            public MainWindow()
            {
                InitializeComponent();            
                PersonName = "default";
                this.DataContext = this;
            }
        
            void button1_Click(object sender, RoutedEventArgs args)
            {            
                MessageBox.Show($"Hello {PersonName}");
            }
        }
        

        MainWindow.xaml

        <StackPanel>
                <TextBox Name="textbox1" 
                         Text="{Binding PersonName, Mode=TwoWay}"
                         />
                <Button Name="button1" Click="button1_Click" Content="Click Me" />
        </StackPanel>
        

        【讨论】:

          猜你喜欢
          • 2011-06-22
          • 2018-03-12
          • 2011-05-31
          • 1970-01-01
          • 1970-01-01
          • 2011-02-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多