Search code examples
c#wpfdata-binding

TextBlock binding updates only once at the beginning


I have a texblock in my .xaml file that I have binded to a property. There are also 2 buttons to ++ and -- the value of that property when they are clicked. The problem is that the binding works only at the start of the application.

When the button is clicked the corresponding command is executed and variable changes its value in the code (so there is no problem with executing the given methods).

However no changes are visible in the UI. I am not sure what might be the cause here, is the UI being somehow blocked? I have always worked using MVVM way and never have had a problem like that with binding.

If more code is needed let me know, didn't want to put a huge wall of code here.

Constructor:

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

        Zoom = 60;

        ZoomPlusSceneCommand = new RelayCommand(ZoomPlus);
        ZoomMinusSceneCommand = new RelayCommand(ZoomMinus);
    }

Handling property changed:

    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Property itself:

    private int zoom;
    public int Zoom
    {
        get { return zoom; }
        set { if (zoom != value) { zoom = value; RaisePropertyChanged("Zoom"); } }
    }

Commands:

    public RelayCommand ZoomPlusSceneCommand { get; set; }
    public RelayCommand ZoomMinusSceneCommand { get; set; }

where RelayCommand is an implementation that I use as a template in most of my projects and they've always been working well (at least in MVVM projects)

Methods executed with the usage of commands when a button is clicked:

    private void ZoomPlus(object o)
    {
        Zoom++;
    }
    private void ZoomMinus(object o)
    {
        Zoom--;
    }

Xaml:

<Window x:Class="Rendering3D.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:Rendering3D"
    Loaded="GetSceneSize"
    ResizeMode="NoResize"
    mc:Ignorable="d"
    Title="MainWindow" Height="550" Width="900">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition x:Name="SceneColumn" Width="*"/>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition Width="40"/>
        <ColumnDefinition Width="10"/>
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="10"/>
        <RowDefinition x:Name="SceneRow" Height="*"/>
        <RowDefinition Height="10"/>
    </Grid.RowDefinitions>

    <Image x:Name="SceneImage" Grid.Column="1" Grid.Row="1" 
           PreviewMouseLeftButtonDown="SceneMouseLeftButtonDown" 
           MouseMove="SceneMouseMove"
           MouseWheel="SceneMouseWheel"/>

    <Grid Grid.Column="3" Grid.Row="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="10"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="10"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Grid.Resources>
            <Style TargetType="Button">
                <Setter Property="Padding" Value="0 5 0 5"/>
            </Style>
        </Grid.Resources>

        <Button Grid.Row="0" Content="+" Command="{Binding ZoomPlusSceneCommand}"/>
        <Button Grid.Row="2" Content="-" Command="{Binding ZoomMinusSceneCommand}"/>
        <Label  Grid.Row="4" Content="Zoom" HorizontalAlignment="Center"/>
        <TextBlock Grid.Row="5" Text="{Binding Zoom}" HorizontalAlignment="Center"/>
    </Grid>
</Grid>

Example of a mouse event:

    private void SceneMouseWheel(object sender, MouseWheelEventArgs e)
    {
        if (e.Delta > 0)
            Zoom++;
        else
            Zoom--;

        return;
    }

Solution

  • Assuming the property gets set, the view should be updated provided that your MainWindow class actually implements INotifyPropertyChanged:

    public partial class MainWindow : Window, INotifyPropertyChanged
    ...