简体   繁体   English

如何编写返回 xaml 切换开关的值的方法?

[英]How can I write a method that returns the value of a of xaml toggle switch?

I have two xaml toggles in separate files that I want to update simultaneously (if one is switched on the other should be too (and vice versa). My first switch in xaml is:我在单独的文件中有两个 xaml 切换,我想同时更新(如果一个打开,另一个也应该打开(反之亦然)。我在 xaml 中的第一个开关是:

<Switch Grid.Column="1" x:Name="toggleSwitch1" IsToggled="true" Toggled="OnToggled"/>

Using C# how can I return a boolean value of this switch so that I can update another switch simultaneously?使用 C# 如何返回此开关的布尔值,以便我可以同时更新另一个开关? Then once retrieving the value, how can I update the xaml of the toggle status for the other switch?然后一旦检索到该值,如何更新另一个开关的切换状态的 xaml?

Your Switch control means, as I can understand, that you using UWP, but I'm not sure.据我所知,您的Switch控制意味着您使用的是 UWP,但我不确定。

Anyway, the idea is to bind both controls IsToggled properties to same property of some ViewModel :无论如何,这个想法是两个控件IsToggled属性绑定到某些ViewModel 的相同属性:

using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace MyWPFApp
{
    public class ControlsViewModel : INotifyPropertyChanged
    {
        private bool switchToggled;
        public bool SwitchToggled
        {
            get => switchToggled;
            set
            {
                switchToggled = value;
                OnPropertyChanged(nameof(SwitchToggled));
            }
        }

        public ControlsViewModel() { }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName]string propertyName = "") => 
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Then in XAML of both Window s set bindings to Switch control (in my example - CheckBox control):然后在两个WindowXAML设置绑定到Switch控件(在我的示例中 - CheckBox控件):

<!-- Window 1 -->
<Window x:Class="MyWPFApp.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyWPFApp"
        mc:Ignorable="d"
        Title="Window 1" Height="100" Width="300">
    <Grid>
        <CheckBox Content="Window1 CheckBox"
                  IsChecked="{Binding SwitchToggled}"/>
                  <!-- Replace IsChecked to IsToggled property -->
    </Grid>
</Window>

<!-- Window 2 -->
<Window x:Class="MyWPFApp.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyWPFApp"
        mc:Ignorable="d"
        Title="Window 2" Height="100" Width="300">
    <Grid>
        <CheckBox Content="Window2 CheckBox" 
                  IsChecked="{Binding SwitchToggled}"/>
                  <!-- Replace IsChecked to IsToggled property -->
    </Grid>
</Window>

Code-behind of both Window s in example is same:示例中两个Window代码隐藏是相同的:

using System.Windows;

namespace MyWPFApp
{
    public partial class Window1 : Window // or public partial class Window2
    {
        public Window1(ControlsViewModel cvm) // or public Window2
        {
            InitializeComponent();
            DataContext = cvm;
        }
    }
}

And when calling that example Window s to show from Main one, you creating ControlsViewModel instance and pass it to both:当调用该示例Window以从 Main显示时,您创建ControlsViewModel实例并将其传递给两者:

using System.Windows;

namespace MyWPFApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var cvm = new ControlsViewModel();

            new Window1(cvm).Show();
            new Window2(cvm).Show();
        }
    }
}

So checking/unchecking (toggle/untoggle) one of them will affect another and vice versa.因此,选中/取消选中(切换/取消切换)其中一个会影响另一个,反之亦然。 Also, you can change SwitchToggled from code somewhere, which would affect both controls too.此外,您可以从某处的代码更改SwitchToggled ,这也会影响两个控件。

在此处输入图片说明

Please note, that this is just example to try explain the idea.请注意,这只是尝试解释这个想法的例子。 More MVVM pattern explanations and examples you can find at MSDN .您可以在MSDN 上找到更多 MVVM 模式解释和示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM