Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

c# - Enable MVVM object only if three other MVVM objects are already enable

I know that this question might be a dummy for an expert in C#, but for me, it's quite confusing to solve.

So, what I have are 4 buttons. All the buttons are 4 different MVVM objects. And what I want is to enable button A only if buttons B,C,D, are enabled.

XAML file

<Window x:Class="TestEnvironment.MainWindow"
        x:Name="MainWindowScreen"
        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:TestEnvironment"
        mc:Ignorable="d"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        Title="My dummy tool"
        Height="720"
        Width="1145"
        ResizeMode="NoResize"
        WindowStartupLocation="CenterScreen"
        BorderBrush="Black"
        BorderThickness="1.5,1.5,1.5,1.5"
        WindowStyle="None">
    <Grid x:Name="GridMain"
              Width="1145"
              Background="White"
              HorizontalAlignment="Center"
              ShowGridLines="False" 
              Grid.Row="1">
            <!--Grid Columns-->
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0"/>
            <ColumnDefinition Width="195"/>
            <ColumnDefinition Width="295"/>
            <ColumnDefinition Width="650"/>
            <ColumnDefinition Width="0"/>
        </Grid.ColumnDefinitions>
        <!--Grid Rows-->
        <Grid.RowDefinitions>
            <RowDefinition Height="0"/>
            <RowDefinition Height="45"/>
            <RowDefinition Height="45"/>
            <RowDefinition Height="45"/>
            <RowDefinition Height="45"/>
            <RowDefinition Height="52"/>
            <RowDefinition Height="400"/>
        </Grid.RowDefinitions>
        <Button 
            x:Name="Button_A"
            Click="Button_A_Click"
            Content="Run"
            IsEnabled="{Binding EnableButtonA}"
            HorizontalAlignment="Left"
            VerticalAlignment="Center"
            Width="80"
            Height="25"
            Margin="135,0,0,0"
            FontSize="10"
            FontWeight="Light"
            Background="{StaticResource CalculationsButtonColor}"
            BorderBrush="{StaticResource CalculationsButtonColor}"
            Grid.Column="3"
            Grid.Row="4"
            Cursor="Hand"/>
        <Button
            x:Name="Button_B"
            Command="{Binding Path=ViewTableCommand}"
            Background="{StaticResource PreviewButtonColor}"
            BorderBrush="{StaticResource PreviewButtonColor}"
            IsEnabled="{Binding Path=EnableButtonB}"
            Content="View"
            Width="80"
            Height="25"
            Margin="545,0,0,0"
            FontSize="10"
            FontWeight="Light"
            HorizontalAlignment="Left"
            VerticalAlignment="Center"
            Grid.Column="3"
            Grid.Row="1"
            Cursor="Hand">
        </Button>
        <Button 
            x:Name="Button_C"
            Command="{Binding Path=ViewTableCommand}"
            Background="{StaticResource PreviewButtonColor}"
            BorderBrush="{StaticResource PreviewButtonColor}"
            IsEnabled="{Binding Path=EnableLButtonC}"
            Content="View"
            Width="80"
            Height="25"
            Margin="545,0,0,0"
            FontSize="10"
            FontWeight="Light"
            HorizontalAlignment="Left"
            VerticalAlignment="Center"
            Grid.Column="3"
            Grid.Row="2"
            Cursor="Hand">
        </Button>
        <Button 
            x:Name="Button_D"
            Command="{Binding Path=ViewTableCommand}"
            Background="{StaticResource PreviewButtonColor}"
            BorderBrush="{StaticResource PreviewButtonColor}"
            IsEnabled="{Binding Path=EnableButtonD}"
            Content="View"
            Width="80"
            Height="25"
            Margin="545,0,0,0"
            FontSize="10"
            FontWeight="Light"
            HorizontalAlignment="Left"
            VerticalAlignment="Center"
            Grid.Column="3"
            Grid.Row="3"
            Cursor="Hand">
        </Button>
    </Grid>
</Window>

.CS file - MVVM model

namespace TestEnvironment
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        //Button B
        private bool _enableButtonB;
        public bool EnableButtonB
        {
            get
            {
                return _enableButtonB;
            }
            set
            {
                _enableButtonB = value;
                OnPropertyChanged("EnableButtonB");
            }
        }

        //Button C
        private bool _enableButtonC;
        public bool EnableButtonC
        {
            get
            {
                return _enableButtonC;
            }
            set
            {
                _enableButtonC = value;
                OnPropertyChanged("EnableButtonC");
            }
        }

        //Button D
        private bool _enableButtonD;
        public bool EnableButtonD
        {
            get
            {
                return _enableButtonD;
            }
            set
            {
                _enableButtonD = value;
                OnPropertyChanged("EnableButtonD");
            }
        }

        //Button A - What I tried so far
        private bool _enableButtonA;
        public bool EnableButtonA
        {
            get
            {
                return _enableButtonA;
            }
            set
            {
                if (EnableButtonB == value && EnableButtonC == value && EnableButtonD == value)

                    _enableButtonA = value;
                OnPropertyChanged("EnableButtonA");
            }
        }
    }
}

Above I presented both the XAML file (with some custom buttons) and the cs file with the MVVM model developed. As mentioned earlier I want to Enable Button A only when the rest of the three buttons are enabled. Otherwise, if at least only 1 is disabled then the Button A should also be disabled.

During my search to solve this, I came across DataTriggers in XAML but I am not confident to judge if DataTriggers is a more efficient approach than MVVM binding. Thus, I stuck around MVVM model. Thanks in advance for your insights and answers.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you could use this:

 //Button B
        private bool _enableButtonB;
        public bool EnableButtonB
        {
            get
            {
                return _enableButtonB;
            }
            set
            {
                _enableButtonB = value;
                OnPropertyChanged("EnableButtonB");
              EnableButtonA=value;
            }
        }
private bool _enableButtonC;
        public bool EnableButtonC
        {
            get
            {
                return _enableButtonC;
            }
            set
            {
                _enableButtonC = value;
                OnPropertyChanged("EnableButtonC");
               EnableButtonA=value;
            }
        }

        //Button D
        private bool _enableButtonD;
        public bool EnableButtonD
        {
            get
            {
                return _enableButtonD;
            }
            set
            {
                _enableButtonD = value;
                OnPropertyChanged("EnableButtonD");
                 EnableButtonA=value;
            }
        }

        //Button A - What I tried so far
        private bool _enableButtonA;
        public bool EnableButtonA
        {
            get
            {
                var enable=EnableButtonB & EnableButtonC & EnableButtonD;
                if(enable!=_enableButtonA) _enableButtonA=enable;
                return _enableButtonA;
            }
            set
            {
                if(!value) _enableButtonA=value;

                else
                    _enableButtonA = EnableButtonB & EnableButtonC & EnableButtonD ;
               
                OnPropertyChanged("EnableButtonA");
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...