ramirbrio
Topic Author
Posts: 6
Joined: 26 Apr 2022, 16:42

ToggleButton with one way binding

26 Sep 2022, 15:10

Hello,
I have a ToggleButton in a view, its IsChecked property is bound to the computed property in OneWay mode. I call the PropertyChanged event if one of the elements is changed. It works fine if I don't click toggle button. But if I click on it, property will not reading anymore.

I have created a small view to show you this issue:
#if UNITY_5_3_OR_NEWER
#define NOESIS
using Noesis;
#else
using System.Windows.Controls;
#endif

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using EventHandler = System.EventHandler;

namespace Testing
{
    public partial class Main : UserControl, INotifyPropertyChanged
    {
        private int _checked = 1;
        private string _lastPressedButton;
        
        public bool IsChecked1 => _checked == 1;

        public bool IsChecked2 => _checked == 2;

        public bool IsChecked3 => _checked == 3;

        public ICommand CheckCommand1 { get; }
        public ICommand CheckCommand2 { get; }
        public ICommand CheckCommand3 { get; }
        public ICommand ChangeCommand { get; }
        
        public string LastPressedButton
        {
            get => _lastPressedButton;
            set
            {
                if (value == _lastPressedButton) return;
                _lastPressedButton = value;
                OnPropertyChanged();
            }
        }

        public Main()
        {
            CheckCommand1 = new ActionCommand(() =>
            {
                LastPressedButton = "1";
                _checked = 1;
                RaiseChecked();
            });
            CheckCommand2 = new ActionCommand(() =>
            {
                LastPressedButton = "2";
                _checked = 2;
                RaiseChecked();
            });
            CheckCommand3 = new ActionCommand(() =>
            {
                LastPressedButton = "3";
                _checked = 3;
                RaiseChecked();
            });
            ChangeCommand = new ActionCommand(() =>
            {
                _checked++;
                if (_checked > 3)
                    _checked = 1;
                RaiseChecked();
            });
            InitializeComponent();
        }

        private void RaiseChecked()
        {
            OnPropertyChanged(nameof(IsChecked1));
            OnPropertyChanged(nameof(IsChecked2));
            OnPropertyChanged(nameof(IsChecked3));
        }

#if NOESIS
        private void InitializeComponent()
        {
            NoesisUnity.LoadComponent(this);
        }
#endif
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        
        public class ActionCommand : ICommand
        {
            private readonly Action _action;

            public ActionCommand(Action action)
            {
                _action = action;
            }

            public bool CanExecute(object parameter)
            {
                return true;
            }

            public void Execute(object parameter)
            {
                _action();
            }

            public event EventHandler CanExecuteChanged;
        }
    }
}
<UserControl x:Class="Testing.Main"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Name="Root">
  <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBlock Text="{Binding LastPressedButton, ElementName=Root, Mode=OneWay}" />
    <ToggleButton IsChecked="{Binding IsChecked1, ElementName=Root, Mode=OneWay}" Command="{Binding CheckCommand1,  ElementName=Root}">
      <TextBlock Text="1" />
    </ToggleButton>
    <ToggleButton IsChecked="{Binding IsChecked2, ElementName=Root, Mode=OneWay}" Command="{Binding CheckCommand2,  ElementName=Root}">
      <TextBlock Text="2" />
    </ToggleButton>
    <ToggleButton IsChecked="{Binding IsChecked3, ElementName=Root, Mode=OneWay}" Command="{Binding CheckCommand3,  ElementName=Root}">
      <TextBlock Text="3" />
    </ToggleButton>
    <Button Command="{Binding ChangeCommand,  ElementName=Root}">
      <TextBlock Text="Next" />
    </Button>
  </StackPanel>
</UserControl>
 
User avatar
sfernandez
Site Admin
Posts: 2664
Joined: 22 Dec 2011, 19:20

Re: ToggleButton with one way binding

27 Sep 2022, 12:39

Hi, this seems a bug in Noesis because I verified it works in Blend just fine. Could you please report it in our bugtracker?
 
ramirbrio
Topic Author
Posts: 6
Joined: 26 Apr 2022, 16:42

Re: ToggleButton with one way binding

27 Sep 2022, 14:01

 
User avatar
sfernandez
Site Admin
Posts: 2664
Joined: 22 Dec 2011, 19:20

Re: ToggleButton with one way binding

27 Sep 2022, 18:19

Thanks for the report, we'll fix it for the next release.

Who is online

Users browsing this forum: Google [Bot] and 2 guests