【问题标题】:How to bind a Property with a textblock From a Class如何将属性与类中的文本块绑定
【发布时间】:2011-07-10 04:42:33
【问题描述】:
 public class myClass : INotifyPropertyChanged
    {
        public string myName(string myNameIs)
        {
            Name = myNameIs;
            return myNameIs;
        }


        public string My = "Hasan"; 
        public string Name {

            get { return My; }
            set
            {
                My = value;
                OnPropertyChanged("Name");
            }

        }

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

。 XAML:

 <TextBlock Height="42" Margin="107,245,0,0" TextWrapping="Wrap" Text="{Binding Name}" VerticalAlignment="Top" HorizontalAlignment="Left" Width="159" DataContext="{Binding Source={StaticResource myClassDataSource}}"/>

这是有效的。但是当我更新属性时它就不起作用了?

【问题讨论】:

    标签: wpf data-binding


    【解决方案1】:

    你的代码相当混乱,你似乎到处都是它。我知道这不是你问的问题,但我想我还是会指出这一点:

    • 您的成员变量被声明为公共 (public string My = "Hasan";)
    • 您的成员变量与其属性的名称完全不同(MyName
    • 你有一个公共属性的设置器,还有一个设置函数(myName(string myNameIs)
    • 您从设置函数返回的值与您传入的值相同

    这是一个如何重写它的示例:

    public class MyClass : INotifyPropertyChanged
    {
        //normal default constructor
        public MyClass()
        {
            _name = "Hasan";
        }
    
        //extra constructor for when you need to set the name to something other than the default
        //although this is really only useful if you have no setter on the Name property
        public MyClass(string name)
        {
            _name = name;
        }
    
        public string Name
        {
    
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged("Name");
            }
    
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                // Raise the PropertyChanged event
                this.PropertyChanged(this, new PropertyChangedEventArgs(
                propertyName));
            }
        }
    
        private string _name;
    }
    

    【讨论】:

    • TextBlock 的 XAML 代码应该是什么样的?我是否需要将 XAML 的 DataContext 设置为此类的实例?
    【解决方案2】:

    您只需要将 TextBlock(或其父级的)DataContext 属性设置为此类的一个实例。 接下来将 Text 属性绑定到支持属性,如下所示

    <TextBlock Text="{Binding Name}"/>
    

    尝试通过一些在线教程(或一本书)而不是试图通过自己的方式来完成。了解 DataBinding 的工作原理后就很容易了。

    更新:一旦我正确格式化了您的问题,我就可以看到您正在使用的 XAML...

    这里的错误是您尝试使用 ElementName 属性(用于通过名称将一个 UI 元素与另一个绑定)。这不是您想要实现的目标。

    【讨论】:

      猜你喜欢
      • 2014-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      • 2019-05-18
      • 2011-05-19
      • 1970-01-01
      相关资源
      最近更新 更多