本文介绍了切换按钮双向绑定不起作用(通用 Windows 平台)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 ToggleButton 上的IsChecked"属性绑定到ModelView.IsEnabled".
ModelView.IsEnabled"始终为false"
但不知何故,切换按钮仍然可以显示为已选中".
绑定有问题吗?

I am trying to bind the "IsChecked" property on the ToggleButton to "ModelView.IsEnabled".
"ModelView.IsEnabled" is always "false"
but somehow the ToggleButton can still show as "Checked".
Is there anything wrong with the binding?

XAML

...
<Page.Resources>
    <ModelView:ModelView x:Key="ModelView"/>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ToggleButton IsChecked="{Binding Source={StaticResource ModelView}, Path=IsEnabled, Mode=TwoWay}">
        <TextBlock >UWP Toggle Button</TextBlock>
    </ToggleButton>
</Grid>
...

ModelView.cs

using...

namespace App2
{
    class ModelView : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public event EventHandler CanExecuteChanged;

        private bool _isEnabled;

        public bool IsEnabled
        {
            get {
                return _isEnabled;
            }
            set
            {
                _isEnabled = false;
                OnPropertyChanged("IsEnabled");
            }
        }

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}

推荐答案

试试这个,它对我有用:1. Xaml 代码改动:

Try this, it worked to me: 1. Xaml code changes:

    <Grid>
    <Grid.DataContext>
        <soHelpProject:MainViewModel/>
    </Grid.DataContext>
    <ToggleButton IsChecked="{Binding IsToggled, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
        <TextBlock >UWP Toggle Button</TextBlock>
    </ToggleButton>
</Grid>

问候,

这篇关于切换按钮双向绑定不起作用(通用 Windows 平台)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-25 15:14