开发者

How to bind counter value to the textblock?

开发者 https://www.devze.com 2023-02-05 15:01 出处:网络
I\'d like to be able to see the counter value in the textblock which incremented on a click of the button. I need to bind it to the textblock. I am wond开发者_如何转开发ering what will be the correct

I'd like to be able to see the counter value in the textblock which incremented on a click of the button. I need to bind it to the textblock. I am wond开发者_如何转开发ering what will be the correct way to do it.

XAML:

<TextBlock Text="{Binding Path=counter}" />
<Button x:Name="Nextbt" Content="Next" Click="ClickNextButton"/>

C#:

private void ClickNextButton(object sender, System.Windows.RoutedEventArgs e){
counter += 1;
if (counter == 1)
{

}
if (counter == 2)
{

}}

Thank you in advance.


1) Make a public property, that returns the counter value. Name it "Counter"
2) Implement INotifyPropertyChanged and call PropertyChanged(new PropertyChangedEventArgs("Counter")) for every change of the counter-value
3) Change your markup as follows:

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

This is only one possiblity of many. This link leads you to an overview of DataBinding. I could imagine that this document will clarify the above steps.

Update

As you wished in your comment, here an example, I assume you are in the main-window. I have changed some things against the above sequence: I have set the DataContext in the constructor. Therefore it is no more necessary to use a relative source for the Binding. Both ways are possible (and both are not very elegant, but to learn WPF-Databinding, they are appropriate). Maybe you try both, the only difference is the binding-declaration and the constructor code.

public partial class MainWindow : Window , INotifyPropertyChanged{    

        public event PropertyChangedEventHandler PropertyChanged;
        int m_counter;

        public MainWindow() {
            InitializeComponent();
            DataContext=this;
        }


        public int Counter {
            get { return m_counter; }
            set {
                if (m_counter != value) {
                    m_counter = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("Counter"));
                }
            }
        }
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) {    
            if (null != PropertyChanged) {        
                PropertyChanged(this,e);    
            }
        }

        private void ClickNextButton(object sender, System.Windows.RoutedEventArgs e){
             Counter += 1; 
             if (Counter == 1) { } 
             if (Counter == 2) { } 
        } 

        Continue with class declaration...

And the XAML:

        <TextBlock Text="{Binding Path=Counter}" /> 
        <Button x:Name="Nextbt" Content="Next" Click="ClickNextButton"/> 


Counter needs to be exposed as a public property in order for your TextBlock to bind to it. Preferably, the property should implement change notification. See MSDN's Data Binding overview for starters!

0

精彩评论

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

关注公众号