开发者

How to bind a Property with a textblock From a Class

 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 Pro开发者_开发技巧pertyChanged 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}}"/>

This is working. But when i update property then it isn`t work?


Your code is rather confusing, you seem to be all over the place with it. I know this isn't the question you asked, but i thought i would point this out anyway:

  • your member variable is declared as public (public string My = "Hasan";)
  • your member variable has a totally different name to its property (My and Name)
  • you have a setter for the public property, and also a setting function (myName(string myNameIs))
  • you are returning the same value from the setting function as what you passed in

Here is an example of how you could rewrite it:

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


You just need to set the TextBlock (or it's parent's) DataContext property to an instance of this class. Next bind the Text property to the backing property like this

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

Try going through a few tutorials online (or a book) instead of trying to forge your way through. It's easy once you get how DataBinding works.

Update: Once I formatted your question correctly, I could see the XAML you are using...

The mistake here is that you're trying to use the ElementName property (which is used to bind one UI element with another by name). This isn't what you're trying to achieve.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜