Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF binding of string property and Button.Content

I want to bind string property with Button.Content.

But why it didn't work?

The data class:

namespace test4
{
    public class Test : INotifyPropertyChanged
    {

         string   _Text = "Begin";

        public string Text
        {
            get{return _Text;}
            protected set { _Text = value; }
        }
        public void Start()
        {
            Text = "Begin";
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(Text));
        }
        public void End()
        {
            Text = "End";
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(Text));
        }


        public event PropertyChangedEventHandler PropertyChanged;
    }
}

The logical code:

  public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        test4.Test ttt = new test4.Test();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            System.Windows.Data.CollectionViewSource testViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("testViewSource")));
            testViewSource.Source = new object[]{ttt};
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (ttt.Text == "Begin")
                ttt.End();
            else
                ttt.Start();
        }
    }

The XAML:

<Window x:Class="test5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:test4" Loaded="Window_Loaded">
    <Window.Resources>
        <CollectionViewSource x:Key="testViewSource" d:DesignSource="{d:DesignInstance my:Test, CreateList=true}" />
    </Window.Resources>
    <Grid DataContext="{StaticResource testViewSource}">
        <Button Content="{Binding Path=Text, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Height="23" HorizontalAlignment="Left" Margin="158,95,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>
like image 442
Begtostudy Avatar asked Jul 21 '10 11:07

Begtostudy


4 Answers

The quotes around Text in the call to the PropertyChangedEventArgs constructor are missing:

Text = "Begin";
if (PropertyChanged != null)
    PropertyChanged(this, new PropertyChangedEventArgs("Text"));
like image 177
Nir Avatar answered Nov 14 '22 05:11

Nir


PropertyChanged wants to see the name of the property that has changed and not the value. Change the event invocation to:

PropertyChanged(this, new PropertyChangedEventArgs("Text")); 

and it will do the job. However I also would change the construction to

public string Text { 
     get{return _Text;} 
     protected set { 
          _Text = value; 
          if(null != PropertyChanged){
                 PropertyChanged(this,new PropertyChangedEventArgs("Text"));
          }   
     } 
} 

and then dont call the PropertyChanged-event from Start and End.

And to go even a step further, create invocation-methods like:

protected virtual void OnPropertyChanged(string propertyName) {
    OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}

protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) {
    if (null != PropertyChanged) {
        PropertyChanged(this,e);
    }
}

and then call them from your property-setter.

like image 23
HCL Avatar answered Nov 14 '22 07:11

HCL


First of all, I used objectdataproviding, but it is not the object "ttt", they are two different things.

Second, PropertyChanged(this,new PropertyChangedEventArgs("Text")); "Text" is the name not the variable.

So, the code is following, may be helpful to others.

The data class:

namespace test3
{
    public class Test : INotifyPropertyChanged
    {

         string   _Text = "Begin";

        public string Text
        {
            get{return _Text;}
            protected set { _Text = value;
            NotifyPropertyChanged("Text");
            }
        }
        public void Start()
        {
            Text = "Begin";

        }
        public void End()
        {
            Text = "End";

        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        } 
    }
}

The logical cs:

  public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            button1.DataContext=ttt;
        } 

        Test ttt = new Test();

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (ttt.Text == "Begin")
                ttt.End();
            else
                ttt.Start();
        }
    }
}

The xaml:

<Window x:Class="test3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:test3"
        Title="MainWindow" Height="350" Width="525" >
    <Grid>
        <Button Content="{Binding Path=Text,UpdateSourceTrigger=PropertyChanged}" Height="23" HorizontalAlignment="Left" Margin="121,69,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>
like image 43
Begtostudy Avatar answered Nov 14 '22 05:11

Begtostudy


You need to use quotes around the property:

public void Start()
{
    Text = "Begin";
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs("Text"));
}

I suggest to rather use the nameof()-method as you cannot rename the property while it's located between quotes:

public void Start()
{
    Text = "Begin";
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(nameof(Text)));
}
like image 35
Faenrig Avatar answered Nov 14 '22 06:11

Faenrig