Home > Software design >  OnPropertyChanged event doesn't toggle Button Visibility
OnPropertyChanged event doesn't toggle Button Visibility

Time:09-17

I have a button which only should be active if the given text above it is a valid URL, i got the correct regex and also a OnPropertyChanged method in which i set the button Visibility to true (it gets converted to visibility in the xaml file)...

Although i set the button visibility to true nothing changes

ViewModel Code:

    private bool m_isSaveButtonVisible = true;

    public bool IsSaveButtonVisible
    {
      get => m_isSaveButtonVisible;
      set 
      {
        m_isSaveButtonVisible = value;
        OnPropertyChanged("???"); //i don't know exactly what to call here?
      }
    }

...

    public event PropertyChangedEventHandler PropertyChanged;

    protected override void OnPropertyChanged(PropertyChangedEventArgs args)
    {
      if (MeetingRole == WebRTCMeetingRole.Join)
      {
        if (Url != m_currentUrl)
        {
          m_currentUrl = Url;
          if (Regex.Match(m_currentUrl, URL_PATTERN, RegexOptions.IgnoreCase).Success)
          {
            PropertyChanged.Invoke(this, e: args); //should set true
          }
          else
          {
             PropertyChanged.Invoke(this, e: args); //should set false
          }
        } 
      }    
    }

XAML Code:

     <TextBlock Text="{x:Static p:Resources.webrtc_url}" Foreground="White" FontSize="18" Margin="0 0 0 10"/>
      <c:WatermarkTextBox attached:FocusExtension.IsFocused="{Binding IsUrlFocused}"
                          Foreground="White" FontSize="19" WatermarkForeground="{x:Static co:Colors.Trout}"
                          Margin="0 0 0 30" Text="{Binding Url, Mode=TwoWay}"
                          Watermark="{x:Static p:Resources.webrtc_url_hint}" WatermarkHorizontalAlignment="Left" HasFocus="True" SelectAll="True"
                          EnterCommand="{Binding SaveCommand, Mode=OneTime}" />
...
<c:IconButton Text="{Binding ConfirmButtonText, Mode=OneWay}" TextAlignment="Center" Foreground="White" FontSize="16"
                    Background="{x:Static co:Colors.DarkOrange}" Margin="0 0 0 8"
                    Command="{Binding SaveCommand, Mode=OneTime}"
                    Visibility="{Binding IsSaveButtonVisible, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}}"/>

Does anybody know why the button visibility isn't set ?

What should happen is, when someone writes a valid URL in the Textfield the savebutton should appear

through the OnPropertyChange i already get noticed when somebody writes something in the textfield the problem is that i cant toggle the button out of this function because it doesn't set the visibility and i don't know why

CodePudding user response:

Property changed just notifies WPF that a property has changed. Nothing more.

so:

public event PropertyChangedEventHandler PropertyChanged;

private bool m_isSaveButtonVisible = true;

public bool IsSaveButtonVisible
{
    get => m_isSaveButtonVisible;
    set 
    {
        m_isSaveButtonVisible = value;
        // if somebody listens to PropertyChanged we tell him IsSaveButtonVisible has changed
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsSaveButtonVisible)));
    }
}

Should be enough.

  • Related