仅当其他三个 MVVM 对象已启用时才启用 MVVM 对象 [英] Enable MVVM object only if three other MVVM objects are already enable
问题描述
我知道这个问题对于 C# 专家来说可能是个傻瓜,但对我来说,解决起来相当混乱.
I know that this question might be a dummy for an expert in C#, but for me, it's quite confusing to solve.
所以,我有 4 个按钮.所有按钮都是 4 个不同的 MVVM 对象.我想要的是只有在启用按钮 B、C、D 时才启用按钮 A.
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 文件
<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 文件 - MVVM 模型
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");
}
}
}
}
上面我展示了 XAML 文件(带有一些自定义按钮)和带有 MVVM 模型的 cs 文件.如前所述,我只想在启用其余三个按钮时才启用 Button A
.否则,如果至少只有 1 个被禁用,则 Button A
也应该被禁用.
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.
在我寻找解决此问题的过程中,我遇到了 XAML 中的 DataTriggers,但我没有信心判断 DataTriggers 是否比 MVVM 绑定更有效.因此,我坚持使用 MVVM 模型.提前感谢您的见解和回答.
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.
推荐答案
你可以使用这个:
//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");
}
}
}
}
这篇关于仅当其他三个 MVVM 对象已启用时才启用 MVVM 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!