[英]Bind several ToggleButton IsEnabled property to other ToggleButtons
I have a page which contains 16 ToggleButtons, 12 of which are by default disabled and will only enable once any of the remaining 4 ToggleButtons is clicked. 我有一个页面,其中包含16个ToggleButton,默认情况下禁用其中12个,并且仅在单击其余4个ToggleButton中的任何一个时才启用。 Is there any way to wire the IsEnabled property of the 12 disabled buttons to the IsChecked property 4 enabled ones?
有什么方法可以将12个禁用按钮的IsEnabled属性连接到IsChecked属性4个启用按钮?
I managed to do this before (and am currently attempting to do it again) using several foreach loops but I imagine that there must be a more elegant way to check if IsChecked == true that doesn't loop through the child elements of their parent grid. 我之前使用几个foreach循环成功做到了这一点(目前正在尝试再次尝试),但我想必须有一种更优雅的方法来检查IsChecked == true是否不会遍历其父级的子元素网格。 Unfortunately the original project was lost during a reformat and no backup of the project file exists so the code has to be rewritten from scratch.
不幸的是,原始项目在重新格式化期间丢失了,并且不存在项目文件的备份,因此必须从头开始重写代码。
start from a blank win8 store app template. 从空白的win8商店应用模板开始。
Find MainPage in solution explorer, completely replace the MainPage XAML with: 在解决方案资源管理器中找到MainPage,将MainPage XAML完全替换为:
<Page
x:Class="StackOverflowMultiButton.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:StackOverflowMultiButton"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<StackPanel Orientation="Horizontal">
<StackPanel>
<ToggleButton x:Name="ToggleButton1" IsChecked="{Binding tbt1_isChecked, Mode=TwoWay}"/>
<ToggleButton x:Name="ToggleButton2" IsChecked="{Binding tbt2_isChecked, Mode=TwoWay}"/>
<ToggleButton x:Name="ToggleButton3" IsChecked="{Binding tbt3_isChecked, Mode=TwoWay}"/>
<ToggleButton x:Name="ToggleButton4" IsChecked="{Binding tbt4_isChecked, Mode=TwoWay}"/>
</StackPanel>
<StackPanel>
<ToggleButton x:Name="ToggleButton5" IsEnabled="{Binding CommonIsEnabled}"/>
<ToggleButton x:Name="ToggleButton6" IsEnabled="{Binding CommonIsEnabled}"/>
<ToggleButton x:Name="ToggleButton7" IsEnabled="{Binding CommonIsEnabled}"/>
<ToggleButton x:Name="ToggleButton8" IsEnabled="{Binding CommonIsEnabled}"/>
<ToggleButton x:Name="ToggleButton9" IsEnabled="{Binding CommonIsEnabled}"/>
<ToggleButton x:Name="ToggleButton10" IsEnabled="{Binding CommonIsEnabled}"/>
<ToggleButton x:Name="ToggleButton11" IsEnabled="{Binding CommonIsEnabled}"/>
<ToggleButton x:Name="ToggleButton12" IsEnabled="{Binding CommonIsEnabled}"/>
<ToggleButton x:Name="ToggleButton13" IsEnabled="{Binding CommonIsEnabled}"/>
<ToggleButton x:Name="ToggleButton14" IsEnabled="{Binding CommonIsEnabled}"/>
<ToggleButton x:Name="ToggleButton15" IsEnabled="{Binding CommonIsEnabled}"/>
<ToggleButton x:Name="ToggleButton16" IsEnabled="{Binding CommonIsEnabled}"/>
</StackPanel>
</StackPanel>
</Grid>
Then the code behind with: 然后,后面的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace StackOverflowMultiButton
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.DataContext = new MainPage_ViewModel();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
}
public class MainPage_ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
bool _tbt1_isChecked = false;
public bool tbt1_isChecked
{
get {
return this._tbt1_isChecked;
}
set {
if (this._tbt1_isChecked == value)
return;
this._tbt1_isChecked = value;
OnPropertyChanged("tbt1_isChecked");
OnPropertyChanged("CommonIsEnabled");
}
}
bool _tbt2_isChecked = false;
public bool tbt2_isChecked
{
get
{
return this._tbt2_isChecked;
}
set
{
if (this._tbt2_isChecked == value)
return;
this._tbt2_isChecked = value;
OnPropertyChanged("tbt2_isChecked");
OnPropertyChanged("CommonIsEnabled");
}
}
bool _tbt3_isChecked = false;
public bool tbt3_isChecked
{
get
{
return this._tbt3_isChecked;
}
set
{
if (this._tbt3_isChecked == value)
return;
this._tbt3_isChecked = value;
OnPropertyChanged("tbt3_isChecked");
OnPropertyChanged("CommonIsEnabled");
}
}
bool _tbt4_isChecked = false;
public bool tbt4_isChecked
{
get
{
return this._tbt4_isChecked;
}
set
{
if (this._tbt4_isChecked == value)
return;
this._tbt4_isChecked = value;
OnPropertyChanged("tbt4_isChecked");
OnPropertyChanged("CommonIsEnabled");
}
}
public bool CommonIsEnabled
{
get
{
return this._tbt1_isChecked ||
this._tbt2_isChecked ||
this._tbt3_isChecked ||
this._tbt4_isChecked;
}
}
}
}
Run the app and see if it does what you need. 运行该应用程序,看看它是否满足您的需求。
Catch the Click-event of the first four buttons. 捕获前四个按钮的Click事件。 Let them call the save eventhandler.
让他们调用保存事件处理程序。
In the eventhandle you write the code: 在事件句柄中,编写代码:
bool enabled = tb1.IsChecked && tb2.IsChecked && tb3.IsChecked && tb4.IsChecked;
tb5.Enabled = tb6.Enabled = ... = tb16.Enabled = enabled;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.