有一个带有2个标签的应用程序,分别是A和B.两个标签都有2个按钮。除了标签外还有1个OK按钮。 (参见截图)
如何实施
当我单击选项卡A按钮1或2时,选项卡B和确定中的按钮将被禁用。 单击“确定”按钮时,将禁用选项卡A或B按钮。 如果单击选项卡B按钮1或2,将禁用选项卡A和确定按钮。 *(禁用大约5-10秒,然后按钮将启用返回)
不使用棱镜框架等。
主窗口XAML
<Window x:Class="TestWpfGlobalCommand.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestWpfGlobalCommand"
Title="MainWindow" Height="350" Width="525">
<StackPanel Orientation="Vertical">
<TabControl>
<TabItem Header="Tab A">
<local:TabA DataContext="{Binding _TabAViewModel}" />
</TabItem>
<TabItem Header="Tab B">
<local:TabB DataContext="{Binding _TabBViewModel}"/>
</TabItem>
</TabControl>
<StackPanel>
<Button Content="Button Main" Width="100" Height="30" Margin="10" Command="{Binding _MainButton}" IsEnabled="{Binding _IsAvailable}" />
</StackPanel>
</StackPanel>
主窗口视图模型C#
public class MainWindowViewModel : INotifyPropertyChanged
{
private ICommand _TabAButton1;
private ICommand _TabAButton2;
private ICommand _TabBButton1;
private ICommand _TabBButton2;
private TabAViewModel _tabAViewModel;
public TabAViewModel _TabAViewModel
{
get { return _tabAViewModel; }
set { _tabAViewModel = value; }
}
private TabBViewModel _tabBViewModel;
public TabBViewModel _TabBViewModel
{
get { return _tabBViewModel; }
set { _tabBViewModel = value; }
}
private ICommand _mainButton;
public ICommand _MainButton
{
get { return _mainButton; }
set { _mainButton = value; }
}
private bool _isAvailable = true;
public bool _IsAvailable
{
get { return _isAvailable; }
set { _isAvailable = value; OnPropertyChanged("_IsAvailable"); }
}
public MainWindowViewModel()
{
_TabAButton1 = new RelayCommand(new Action<object>(DisplayTabAButton1Message));
_TabAButton2 = new RelayCommand(new Action<object>(DisplayTabAButton2Message));
_TabBButton1 = new RelayCommand(new Action<object>(DisplayTabBButton1Message));
_TabBButton2 = new RelayCommand(new Action<object>(DisplayTabBButton2Message));
_MainButton = new RelayCommand(new Action<object>(ShowMainButtonMessage));
_TabAViewModel = new TabAViewModel()
{
_TabAButton1 = this._TabAButton1,
_TabAButton2 = this._TabAButton1,
};
_TabBViewModel = new TabBViewModel()
{
_TabBButton1 = this._TabBButton1,
_TabBButton2 = this._TabBButton1,
};
DisableButtons(true);
}
private void ShowMainButtonMessage(object obj)
{
MessageBox.Show("Main Button Message.");
}
private async void DisplayTabAButton1Message(object obj)
{
DisableButtons(false);
MessageBox.Show("Tab A Button 1.");
await Task.Delay(3000);
DisableButtons(true);
}
private async void DisplayTabAButton2Message(object obj)
{
DisableButtons(false);
MessageBox.Show("Tab A Button 2.");
await Task.Delay(3000);
DisableButtons(true);
}
private async void DisplayTabBButton1Message(object obj)
{
DisableButtons(false);
MessageBox.Show("Tab B Button 2.");
await Task.Delay(3000);
DisableButtons(true);
}
private async void DisplayTabBButton2Message(object obj)
{
DisableButtons(false);
MessageBox.Show("Tab B Button 1.");
await Task.Delay(3000);
DisableButtons(true);
}
private void DisableButtons(bool value)
{
_tabAViewModel._IsAvailable = value;
_tabBViewModel._IsAvailable = value;
_IsAvailable = value;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string Property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(Property));
}
}
}
标签A XAML
<UserControl x:Class="TestWpfGlobalCommand.TabA"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<TextBox Width="100" Height="30" Margin="10" />
<TextBox Width="100" Height="30" Margin="10" />
</StackPanel>
<Button Content="Button A 1" Width="100" Height="30" Margin="10" Command="{Binding _TabAButton1}" IsEnabled="{Binding _IsAvailable}" />
<Button Content="Button A 2" Width="100" Height="30" Margin="10" Command="{Binding _TabAButton2}" IsEnabled="{Binding _IsAvailable}" />
</StackPanel>
</Grid>
选项卡A视图模型C#
public class TabAViewModel : INotifyPropertyChanged
{
private ICommand _tabAButton1;
public ICommand _TabAButton1
{
get { return _tabAButton1; }
set { _tabAButton1 = value; }
}
private ICommand _tabAButton2;
public ICommand _TabAButton2
{
get { return _tabAButton2; }
set { _tabAButton2 = value; }
}
private bool _isAvailable;
public bool _IsAvailable
{
get { return _isAvailable; }
set { _isAvailable = value; OnPropertyChanged("_IsAvailable"); }
}
public TabAViewModel()
{
//_TabAButton1 = new RelayCommand(new Action<object>(DisplayTabAButton1Message));
//_TabAButton2 = new RelayCommand(new Action<object>(DisplayTabAButton2Message));
}
//private void DisplayTabAButton1Message(object obj)
//{
// MessageBox.Show("Tab A Button 1.");
//}
//private async void DisplayTabAButton2Message(object obj)
//{
// //_IsAvailable = false;
// MessageBox.Show("Tab A Button 2.");
// await Task.Delay(5000);
// //_IsAvailable = true;
//}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string Property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(Property));
}
}
}
我已经做了一个样本,但是有没有改进结构或应用设计模式? 感谢