開始。
<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>
然後後面的代碼有:在解決方案資源管理
查找的MainPage,完全取代的MainPage XAML
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;
}
}
}
}
運行應用程序,看看它是否做了什麼你需要。
您可以通過類似於'IsEnabled = {Binding IsChecked,ElementName = SomeOtherToggleButton}'的方式在XAML中執行此操作。但是,如果您希望儘可能簡化操作,則應考慮使用MVVM模式開發應用程序;在視圖模型中對這種類型的行爲進行建模非常簡單。 – dlev 2013-04-28 17:48:52
我意識到綁定語法,但如果我錯了,請糾正我,如果綁定到單個元素,那麼這不是唯一的方法嗎?我需要綁定到4個ToggleButtons,以便每當這4個選中的任何一個被選中時,剩下的12個被啓用。我也知道,MVVM會讓我的生活變得如此簡單,但時間限制現在不允許它。 – Martin 2013-04-28 17:54:33
啊,我不知道你需要所有四個人都活躍。您仍然可以使用in-XAML綁定:只需使用'MultiBinding',輸入是來自四個切換按鈕的'IsChecked'狀態。這個頁面(http://tech.pro/tutorial/809/wpf-tutorial-using-multibindings)有一個關於如何設置它的快速教程(以及如何編寫你需要的轉換器)。 – dlev 2013-04-28 18:00:04