IsChecked Checkbox绑定未响应

时间:2018-10-02 11:59:25

标签: c# wpf checkbox binding

我在MenuItem上有一个这样的复选框

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

我只想在bool更改时在MenuItem BUT上将复选框设置为true或false,我看到正在调用getter但复选框状态没有更改。

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

        isFullScreen = value;
        OnPropertyChanged();
    }
}

我将XAML的其他部分绑定到IsFullScreen(而不是复选框),并且工作正常。

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

(上面的代码有效)

我在这里看到了很多解决方案,但是没有一个帮助我

正确的方法是什么?为什么我的IsChecked不能正常工作?

编辑
我的OnPropertyChanged

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

编辑2
尝试过:Mode = TwoWay,转换器,布尔型
我在点击事件中对后面的代码设置了true或false。

带有bool的代码

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

            isFullScreen = value;
            OnPropertyChanged();
        }
    }

编辑3

全屏点击的一部分

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");
    }
}

已经尝试使用e.Handled = true

1 个答案:

答案 0 :(得分:1)

尝试将XML更改为:  IsChecked =“ {Binding Path = IsFullScreen,Mode = TwoWay}”>

看看这个问题,我认为它将帮助您: WPF CheckBox TwoWay Binding not working