0

I've just started learning WPF desktop application. I've written some let say easy code below, to excercise binding operation.

The problem is: I wanted type sth in TextBox and see it simultaneously in TextBlock, but after compiling and running app, controls on form do not behave as I described.

Can anybody help me to fix it?

MainWindow.xaml:

<Window x:Class="Napisy.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Napisy"
        xmlns:mv="clr-namespace:Napisy.ModelWidoku"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <mv:NapisyModelWidoku x:Key="napisyModelWidoku"/>
    </Window.Resources>  

    <Grid DataContext="{StaticResource napisyModelWidoku}">
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>
        <TextBox Grid.Row="1" Margin="10,10,10,10" Text="{Binding Path=Tekst,Mode=TwoWay}"/>
        <TextBlock Grid.Row="2" Margin="10,10,10,10" Text="{Binding Path=Wyswietl,Mode=OneWay}"/>
    </Grid>
</Window>

ViewModel code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Napisy.Model;
using System.ComponentModel;

namespace Napisy.ModelWidoku
{
    public class NapisyModelWidoku : INotifyPropertyChanged
    {
        NapisyModel model = new NapisyModel();

        public string Tekst
        {
            get
            {
                return model.Tekst;
            }
            set
            {
                model.Tekst = value;
                OnPropertyChanged(nameof(Tekst));
                OnPropertyChanged(nameof(Wyswietl));
            }
        }
        public string Wyswietl
        {
            get
            {
                return model.Tekst;
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged(string nazwa)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nazwa));
        }
    }
}

Model Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Napisy.Model
{
    public class NapisyModel
    {
        public string Tekst { get; set; }
    }
}

EDIT:

  • Problem description,
  • before class NapisyModelWidoku, acces modificator public added,
  • added OnPropertyChanged(nameof(Tekst));
  • instead OnPropertyChanged("Wyswietl"); used OnPropertyChanged(nameof(Wyswietl));

After typing text into TextBox still TextBlock not refresh automatically. Still hope I receive tips. Thanks

2
  • "do not behave as I intended" - is not really a good description of the problem. What doesn't work? I can see you forgot to rise notification for Tekst itself: OnPropertyChanged(nameof(Tekst)); Commented Jan 4, 2017 at 12:42
  • EDIT: "class NapisyModelWidoku" modified to "public class NapisyModelWidoku". Still not working. Commented Jan 4, 2017 at 12:43

2 Answers 2

0

Along with adding UpdateSourceTrigger in both bindings, make below change also,

 public string Tekst
    {
        get
        {
            return model.Tekst;
        }
        set
        {
            model.Tekst = value;
            OnPropertyChanged("Tekst");
            OnPropertyChanged("Wyswietl");
        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

I implemented all your suggestions. There is still bug in code.
have you added UpdateSoureTrigger in both bindings? because it is working perfectly for me.
I have added only to TextBlock by mistake. When I placed to both TextBox and TextBlock it works. Thank you !:)
Actually you don't have to add UpdatesourceTrigger to texblock : it is useless as you never type anyhting into it. Your original problem seems to be due to the fact that The default behavior for textbox is to send the data to the binding when the control loose focus. The updateSourceTrigger ensure that the binding is refreshed each time the input changes. Carreful touht because then your binding will be evaluated for every caracter you input in the textbox. To leverage this problem the later version of the framework enable you to defer the binding evaluation.
0

I ran your code and it does not work because PropertyChanged is null. You have to set the datacontext of your view so that the PropertyChangedEventHandler can be binded.

Add in your code behind, i-e MainWindow.xaml.cs

public MainWindow()
{
     InitializeComponent();
     this.DataContext = new NapisyModelWidoku();
}

3 Comments

No change after adding UpdateSourceTrigger=PropertyChanged
Yes I figured out the problem let me make an edit to my answer
or you can set the DataContext to an object of NapisyModelWidoku in the xaml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.