wpf toggleSwitch 的只读属性

xml code

---------------------------------------------

<Page

    x:Class="UWPDemo.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:UWPDemo"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>

        <ToggleSwitch IsOn="{x:Bind isno, Mode=TwoWay}">on </ToggleSwitch>

        <Button Content="Button" Click="Button_Click" Margin="524,601,0,0" VerticalAlignment="Top" Width="434"/>

    </Grid>

</Page>

C# code

-----------------------------------

public sealed partial class MainPage : Page, INotifyPropertyChanged

    {

        private bool _isno;

        public event PropertyChangedEventHandler PropertyChanged;

        public bool isno

        {

            set

            {

                if(PropertyChanged!=null)

                {

                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(isno)));

                }

            }

            get

            {

                return _isno;

            }

        }

        public MainPage()

        {

            this.InitializeComponent();

        }

        private void Button_Click(object sender, RoutedEventArgs e)

        {

            _isno=!_isno;

            if (PropertyChanged != null)

            {

                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(isno)));

            }

        }

    }

原文地址:https://www.cnblogs.com/bruce1992/p/14244146.html