0
votes

I have a checkbox on MenuItem like this

<MenuItem Header="FullScreen" 
          Click="FullScreen"
          IsChecked="{Binding IsFullScreen}">
</MenuItem>

I just want to set the checkbox with true or false on my MenuItem BUT when my bool changes, I see the getter being called but checkbox state doesn't change.

private bool? isFullScreen;
public bool? IsFullScreen
{
    get { return isFullScreen ?? false; }
    set
    {
        if (isFullScreen == value)
            return;

        isFullScreen = value;
        OnPropertyChanged();
    }
}

I've other part of XAML binding to IsFullScreen (not a checkbox) and works fine.

<DataTrigger Binding="{Binding IsFullScreen}" Value="True">
    <Setter Property="Visibility" Value="Collapsed" TargetName="MainDock"/>
    <Setter Property="BorderThickness" Value="0 0 0 0"/>
</DataTrigger>

(code above is working)

I saw a lot of solutions here but none helped me

What is the right way? Why my IsChecked don't work as should?

EDIT
My OnPropertyChanged

    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

EDIT 2
Tried with: Mode=TwoWay, Converter, bool
Im setting true or false on code behind in click event.

Code with bool

    public bool IsFullScreen
    {
        get { return isFullScreen; }
        set
        {
            if (isFullScreen == value)
                return;

            isFullScreen = value;
            OnPropertyChanged();
        }
    }

EDIT 3

Part of fullscreen click

private void FullScreen(object sender, RoutedEventArgs e)
{
    if (this.WindowStyle == WindowStyle.None)
    {
        this.WindowStyle = WindowStyle.SingleBorderWindow;
        this.ResizeMode = ResizeMode.CanResize;
        this.WindowState = WindowState.Normal;

        IsFullScreen = false;
        LogReport.Instance.Log("Modo FullScreen foi desativado");
    }
    else
    {
        this.WindowState = WindowState.Normal; // if don't set dont works well
        this.WindowStyle = WindowStyle.None;
        this.ResizeMode = ResizeMode.NoResize;
        this.WindowState = WindowState.Maximized;

        IsFullScreen = true;
        LogReport.Instance.Log("Modo FullScreen foi ativado");
    }
}

Already tried with e.Handled = true

1
The code what you have provided does not change the state of a checkbox and since isFullScreen is null by default, you cannot see the checkbox to checked state. But check below answer and try with your whole solution, I think Mode="TwoWay" should work out.G K
Im setting in codebehind, sorry for lack of informationKevin Kouketsu
You code works by my side. I can't reproduce the issue. Are you sure, that IsFullScreen from event handler is the same from ViewModel. Do you use Window as ViewModel? What is DataContext for your window?Rekshino
I really don't know why but works in one build and after doens't work anymore. On my test project works well. Datacontext its set to 'this'Kevin Kouketsu
I found the problem. My ContextMenu it's opened manually and when I use the right click works normal. Why is contextmenu having this behavior? I need to set datacontext with this manuallyKevin Kouketsu

1 Answers

1
votes

Try to change your XML to: IsChecked="{Binding Path=IsFullScreen, Mode=TwoWay}">

And take a look in this question, I think it will help you: WPF CheckBox TwoWay Binding not working