首页 新闻 会员 周边 捐助

是否可以只用xaml改变button中绑定viewmodel的某个bool变量的值呢?

0
悬赏园豆:50 [已解决问题] 解决于 2022-04-05 16:30

我想通过单击button按钮,使得绑定的bool变量在true和false之间切换。因为我有多个bool变量需要用对应按钮控制,所以想做一个能操作绑定bool变量的自定义的按钮控件,但是一直没能做出来。请问我是否可以不使用command,只用xaml就能实现呢?如果可以,我需要怎么做?

临雪的主页 临雪 | 初学一级 | 园豆:106
提问于:2022-03-31 14:12
< >
分享
最佳答案
1

把checkbox的样式改改,改成button的样子。不过这样还不如用command快捷呢,为什么不愿意使用comamnd呢?

收获园豆:50
会长 | 专家六级 |园豆:12463 | 2022-03-31 14:37

也没有不愿意用command,只是因为不会,没有想到怎么用command解决这个问题。。。是写一个操作bool的command,然后把需要操作的bool变量的引用传进去吗?

临雪 | 园豆:106 (初学一级) | 2022-03-31 21:43

@isuperSun: 我写了一个demo,你看看:点我下载

会长 | 园豆:12463 (专家六级) | 2022-04-01 09:25

@会长: 你好,我看了demo。我能想到的方式就是这种,但是如果我有很多个绑定了不同bool变量的button时,我觉得还要写很多个对应的command感觉很笨,所以有没有可能只用一个command,让不同的bool变量作为参数传进去的方式,但这样我不知道能不能实现。

临雪 | 园豆:106 (初学一级) | 2022-04-01 14:33

@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里的代码,可以看效果

会长 | 园豆:12463 (专家六级) | 2022-04-01 15:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册