把checkbox的样式改改,改成button的样子。不过这样还不如用command快捷呢,为什么不愿意使用comamnd呢?
也没有不愿意用command,只是因为不会,没有想到怎么用command解决这个问题。。。是写一个操作bool的command,然后把需要操作的bool变量的引用传进去吗?
@isuperSun: 我写了一个demo,你看看:点我下载
@会长: 你好,我看了demo。我能想到的方式就是这种,但是如果我有很多个绑定了不同bool变量的button时,我觉得还要写很多个对应的command感觉很笨,所以有没有可能只用一个command,让不同的bool变量作为参数传进去的方式,但这样我不知道能不能实现。
@isuperSun: 可以用CommandParameter
传递参数。我改了下:
<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="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="48"></RowDefinition>
<RowDefinition Height="48"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Button Content="Click Me" Command="{Binding ClickCommand}" CommandParameter="Checked1" Width="100" Height="32" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5"></Button>
<Button Content="Click Me" Command="{Binding ClickCommand}" CommandParameter="Checked2" Width="100" Height="32" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5"></Button>
<Button Content="Click Me" Command="{Binding ClickCommand}" CommandParameter="Checked3" Width="100" Height="32" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5"></Button>
<Button Content="Click Me" Command="{Binding ClickCommand}" CommandParameter="Checked4" Width="100" Height="32" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5"></Button>
<Button Content="Click Me" Command="{Binding ClickCommand}" CommandParameter="Checked5" Width="100" Height="32" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5"></Button>
<Button Content="Click Me" Command="{Binding ClickCommand}" CommandParameter="Checked6" Width="100" Height="32" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5"></Button>
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<CheckBox IsEnabled="False" IsChecked="{Binding Checked1}" Content="Checked" VerticalAlignment="Center" Margin="20,5"></CheckBox>
<CheckBox IsEnabled="False" IsChecked="{Binding Checked2}" Content="Checked" VerticalAlignment="Center" Margin="20,5"></CheckBox>
<CheckBox IsEnabled="False" IsChecked="{Binding Checked3}" Content="Checked" VerticalAlignment="Center" Margin="20,5"></CheckBox>
<CheckBox IsEnabled="False" IsChecked="{Binding Checked4}" Content="Checked" VerticalAlignment="Center" Margin="20,5"></CheckBox>
<CheckBox IsEnabled="False" IsChecked="{Binding Checked5}" Content="Checked" VerticalAlignment="Center" Margin="20,5"></CheckBox>
<CheckBox IsEnabled="False" IsChecked="{Binding Checked6}" Content="Checked" VerticalAlignment="Center" Margin="20,5"></CheckBox>
</StackPanel>
</Grid>
</Window>
VM:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp
{
public class MainWindowVM: INotifyPropertyChanged
{
public MainWindowVM()
{
ClickCommand = new MyCommand(Click);
}
private void Click(object obj)
{
string checkedName = obj as string;
switch (checkedName)
{
case "Checked1":Checked1 = !Checked1;break;
case "Checked2":Checked2 = !Checked2;break;
case "Checked3":Checked3 = !Checked3;break;
case "Checked4":Checked4 = !Checked4;break;
case "Checked5":Checked5 = !Checked5;break;
case "Checked6":Checked6 = !Checked6;break;
default: break;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private bool _checked1;
public bool Checked1
{
get { return _checked1; }
set
{
_checked1 = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Checked1)));
}
}
private bool _checked2;
public bool Checked2
{
get { return _checked2; }
set
{
_checked2 = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Checked2)));
}
}
private bool _checked3;
public bool Checked3
{
get { return _checked3; }
set
{
_checked3 = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Checked3)));
}
}
private bool _checked4;
public bool Checked4
{
get { return _checked4; }
set
{
_checked4 = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Checked4)));
}
}
private bool _checked5;
public bool Checked5
{
get { return _checked5; }
set
{
_checked5 = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Checked5)));
}
}
private bool _checked6;
public bool Checked6
{
get { return _checked6; }
set
{
_checked6 = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Checked6)));
}
}
public MyCommand ClickCommand { get; set; }
}
}
用此代码替换我demo里的代码,可以看效果