firepower91
firepower91

Reputation: 413

TextBlock binding doesnt work

I would like to make a simple WPF app, which contains a Button and a TextBlock in the view. I would like, when i click on the button, write "Hello" to the TextBlock.

Here is my View:

<Window x:Class="PropertyTest.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">
    <Grid>
    <Button Name="ButtonIncreaser" Content="Button"  Command="{Binding CalculateCommand}" CommandParameter="+" HorizontalAlignment="Left" Height="23" Margin="400,206,0,0" VerticalAlignment="Top" Width="75"/>
    <TextBlock Name="TextB" HorizontalAlignment="Left" Height="23" Margin="56,206,0,0" TextWrapping="Wrap" Text="{Binding Szoveg,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="292"/>
    </Grid>
</Window>

Thats MainWindow.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        ViewModel m = new ViewModel();
        this.DataContext = m;
        this.Show();
    }
}

And thats my viewmodel:

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public DelegateCommand CalculateCommand { get; private set; }
    public ViewModel()
    {
        CalculateCommand = new DelegateCommand(param => Calculate(param.ToString()));
    }

    public void Calculate(string param)
    {
        _str = "Hello";
    }

    private string _str;
    private string Szoveg
    {
        get
        {
            return _str;
        }
        set
        {
            _str = value;
            OnPropertyChanged("Szoveg");
        }
    }


    public void OnPropertyChanged(String name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        } 
    }
}

What do i do wrong? Thanks!

Upvotes: 0

Views: 74

Answers (1)

Tim
Tim

Reputation: 15247

In your Calculate function, you're directly setting your private data member _str. That doesn't cause the OnPropertyChanged function to get called saying that the value of Szoveg has changed.

Just change that line to be

Szoveg = "Hello" 

and you should be good.

As XAMIMAX pointed out in the comments, you need to change the Szoveg property to be a public property as well - per MSDN:

The properties you use as binding source properties for a binding must be public properties of your class. Explicitly defined interface properties cannot be accessed for binding purposes, nor can protected, private, internal, or virtual properties that have no base implementation.

Additionally, you don't really need the UpdateSourceTrigger=PropertyChanged part of your binding - that makes sense in some scenarios for a TextBox where the user is editing the contents of the TextBox. But for a TextBlock it doesn't really make as much sense and is unnecessary in the vast majority of cases.

Upvotes: 3

Related Questions