所以我有 5 個復選框,并且希望在單擊一個按鈕時在每個復選框上獲得不同的檢查狀態。因此,當我的 CheckBoxes 未選中并且我單擊 Button 時,CheckBoxes 被選中,當 CheckBoxes 被選中并且我單擊 Button 時它未被選中。
我遇到的問題是,我只想使用 XAML 來完成這項作業。背后的代碼不是問題,但我想讓它只與系結一起作業。
uj5u.com熱心網友回復:
所以,正如你所說:
我想讓它只與系結一起作業
你有Window幾個CheckBoxes 和Button,它切換檢查狀態。CheckBox使用 Bindings設定es 到某些AllChecked屬性(我們將在代碼隱藏中創建它):
XAML:
<Window x:Class="WpfApp.MainWindow"
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:WpfApp"
mc:Ignorable="d"
Title="MainWindow"
Height="150"
Width="400"
Name="mainWindow"
KeyDown="OnKeyDown">
<Window.Resources>
<local:BooleanToStringConverter x:Key="BoolToStringConverter"/>
</Window.Resources>
<Grid>
<StackPanel x:Name="checkBoxPanel"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="10,10,0,0">
<!-- Binding CheckBoxes and disabling them -->
<CheckBox IsChecked="{Binding ElementName=mainWindow,
Path=AllChecked,
UpdateSourceTrigger=PropertyChanged}"
Content="CheckBox1"
IsEnabled="False"/>
<CheckBox IsChecked="{Binding ElementName=mainWindow,
Path=AllChecked,
UpdateSourceTrigger=PropertyChanged}"
Content="CheckBox2"
IsEnabled="False"/>
<!-- Same 3 other CheckBoxes -->
</StackPanel>
<!-- Button also binded to same property. -->
<!-- With Converter its Text change between "Check All" and "Uncheck All" -->
<Button x:Name="btnCheckUncheckAll"
Content="{Binding ElementName=mainWindow,
Path=AllChecked,
UpdateSourceTrigger=PropertyChanged,
Converter={StaticResource BoolToStringConverter},
ConverterParameter=btnCheckUncheckAll}"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Margin="0,10,0,0"
Width="100"
Height="32"
Click="ButtonCheckUncheckAll_Click"/>
</Grid>
</Window>
CheckBox由于您的評論“...我不想單擊復選框”,示例中的所有es 都被禁用。正如@Shrimperator 所注意到的那樣,檢查一個將導致檢查所有CheckBoxes,因為它們系結到相同的源(這是他們禁用的另一個原因)。BooleanToStringConverter使用AllChecked屬性值來設定“全部檢查”或“取消全部檢查”按鈕文本(請參閱其代碼隱藏部分的代碼)。我將按鈕x:Name作為引數傳遞給轉換器,以便將來能夠將轉換器用于其他不同的按鈕,而不僅僅是這個。
窗戶外觀:

Code-behind:
using System;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
namespace WpfApp
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private bool allChecked;
// Single property each CheckBox binded to
public bool AllChecked
{
get => allChecked;
set
{
allChecked = value;
// Notify binded controls that we changed value
OnPropertyChanged(nameof(AllChecked));
}
}
// INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
// Constructor
public MainWindow() => InitializeComponent();
// If button control clicked - check state on each CheckBox will change
private void ButtonCheckUncheckAll_Click(object sender, RoutedEventArgs e) =>
SwitchCheckState();
private void OnKeyDown(object sender, KeyEventArgs e)
{
// If C button pressed - check state on each CheckBox will change
if (e.Key == Key.C)
SwitchCheckState();
}
// Simply switch between True and False
private void SwitchCheckState() =>
AllChecked = !AllChecked;
}
public class BooleanToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (parameter is string buttonName && buttonName == "btnCheckUncheckAll")
return (bool)value == true ? "Uncheck all" : "Check all";
return string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) =>
string.Empty;
}
}
So we have simple SwitchCheckState method to change AllChecked property between True and False. It is called by Button click (ButtonCheckUncheckAll_Click handler) or by C(heck) key press (OnKeyDown handler on whole Window). We change just AllChecked property value in any way we wish, when INotifyPropertyChanged and Bindings do other magic somewhere behind.
It is simplified example, without ViewModels and DataContexting. Post a comment if you have other questions, improvement suggestions or need some another explanations.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/354191.html
