所以我有 5 個複選框,並且希望在單擊一個按鈕時在每個複選框上獲得不同的檢查狀態。因此,當我的 CheckBoxes 未選中並且我單擊 Button 時,CheckBoxes 被選中,當 CheckBoxes 被選中並且我單擊 Button 時它未被選中。
我遇到的問題是,我只想使用 XAML 來完成這項工作。背後的代碼不是問題,但我想讓它只與綁定一起工作。
所以,正如你所說:
我想讓它只與綁定一起工作
你有Window
幾個CheckBox
es 和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注意到的那樣,檢查一個將導致檢查所有CheckBox
es,因為它們綁定到相同的源(這是他們禁用的另一個原因)。BooleanToStringConverter
使用AllChecked
屬性值來設置“全部檢查”或“取消全部檢查”按鈕文本(請參閱其代碼隱藏部分的代碼)。我將按鈕x:Name
作為參數傳遞給轉換器,以便將來能夠將轉換器用於其他不同的按鈕,而不僅僅是這個。
窗戶外觀:
代碼隱藏:
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;
}
}
所以我們有簡單的SwitchCheckState
方法來改變和AllChecked
之間的屬性。它由按鈕單擊(處理程序)或C(哎呀)按鍵(整個窗口上的處理程序)調用。我們以任何我們希望的方式改變屬性值,當和綁定在後面的某個地方做其他魔術時。True
False
ButtonCheckUncheckAll_Click
OnKeyDown
AllChecked
INotifyPropertyChanged
它是一個簡化的例子,沒有 ViewModels 和 DataContexting。如果您有其他問題、改進建議或需要其他解釋,請發表評論。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句