我有一个WPF应用程序,在一段时间过去之前不允许我单击按钮,但这种行为是随机的,只会在某些时候发生。大部分时间窗口呈现,我可以立即点击所述按钮。
主窗口:
<views:StatusBox DockPanel.Dock="Left" Visibility="{Binding Editing, Converter={StaticResource BoolToVisibleConverter}}" />
</DockPanel>
用户控制:
<StackPanel Orientation="Horizontal">
<!--Read Button-->
<Button Content="Read " x:Name="ReadBtn" Command="{Binding ReadCMD}" Padding="10,5" Margin= "10,0" IsEnabled="{Binding ReadEnabled}" />
<!--Save Button-->
<Button Content="Save " x:Name="SaveBtn" Command="{Binding SaveCMD}" Padding="10,5" Margin= "10,0" IsEnabled="{Binding SaveEnabled}"
Background="{StaticResource {x:Static SystemColors.ControlLightBrushKey}}">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSaveNeeded}" Value="Yes">
<DataTrigger.EnterActions>
<BeginStoryboard x:Name="FlashBackground">
<Storyboard BeginTime="00:00:00" RepeatBehavior="Forever" >
<ColorAnimation Storyboard.TargetProperty="Background.Color" Duration="00:00:00.25" AutoReverse="True" To="Red" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<StopStoryboard BeginStoryboardName="FlashBackground" />
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<!--Load Button-->
<Button Content="Load " x:Name="LoadBtn" Command="{Binding LoadCMD}" Padding="10,5" Margin= "10,0" IsEnabled="{Binding LoadEnabled}" />
<!--Print Button-->
<Button Content="Print" x:Name="PrintBtn" Command="{Binding PrintCMD}" Padding="10,5" Margin= "10,0" IsEnabled="{Binding PrintEnabled}" />
<!--Exit Button-->
<Button Content="Exit" x:Name="ExitBtn" Command="{Binding ExitCMD}" Padding="10,5" Margin= "10,0" />
</StackPanel>
查看模型
private bool _tab1_Selected;
public bool tab1_Selected
{
get
{
return _tab1_Selected;
}
set
{
_tab1_Selected = value;
if (value)
{
CommMode = (byte)PacketType.tab1_DUMP;
if (tab1needsSaving)
{
IsSaveNeeded = SaveNeeded.Yes;
}
else
{
IsSaveNeeded = SaveNeeded.No;
}
}
NotifyPropertyChanged("tab1_Selected");
NotifyPropertyChanged("SaveEnabled");
NotifyPropertyChanged("ProgramEnabled");
NotifyPropertyChanged("Editing");
}
}
private bool _tab2_Selected;
public bool tab2_Selected
{
get
{
return _tab2_Selected;
}
set
{
_tab2_Selected = value;
if (value)
{
CommMode = (byte)PacketType.tab2_DUMP;
if (tab2needsSaving)
{
IsSaveNeeded = SaveNeeded.Yes;
}
else
{
IsSaveNeeded = SaveNeeded.No;
}
}
NotifyPropertyChanged("tab2_Selected");
NotifyPropertyChanged("SaveEnabled");
NotifyPropertyChanged("ProgramEnabled");
NotifyPropertyChanged("Editing");
}
}
public bool Editing
{
get
{
return tab2_Selected || tab1_Selected;
}
}
private bool _saveEnabled;
public bool SaveEnabled
{
get
{
//return _saveEnabled;
return ((tab1_Selected && tab1Data != null && tab1Data.Count > 0) || (tab2_Selected && tab2Data != null && tab2Data.Count > 0) && _saveEnabled);
}
set
{
_saveEnabled = value;
NotifyPropertyChanged("SaveEnabled");
}
}
private bool _programEnabled;
public bool ProgramEnabled
{
get
{
//return _saveEnabled;
return ((tab1_Selected && tab1Data != null && tab1Data.Count > 0) || (tab2_Selected && tab2Data != null && tab2Data.Count > 0) && _programEnabled);
}
set
{
_programEnabled = value;
NotifyPropertyChanged("ProgramEnabled");
}
}
命令:
public ICommand ReadCMD { get; set; }
public ICommand SaveCMD { get; set; }
public ICommand LoadCMD { get; set; }
public ICommand ProgramCMD { get; set; }
public ICommand PrintCMD { get; set; }
public ICommand ExitCMD { get; set; }
// In MainVM constructor
ReadCMD = new RelayCommand(ReadSettings);
SaveCMD = new RelayCommand(SaveSettings);
LoadCMD = new RelayCommand(LoadSettings);
ProgramCMD = new RelayCommand(ProgramSettings);
PrintCMD = new RelayCommand(PrintSettings);
ExitCMD = new RelayCommand(ExitProgram);
中继命令类:
public class RelayCommand : ICommand
{
private Action<object> execute;
private Predicate<object> canExecute;
private event EventHandler CanExecuteChangedInternal;
public RelayCommand(Action<object> execute)
: this(execute, DefaultCanExecute)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
}
if (canExecute == null)
{
throw new ArgumentNullException("canExecute");
}
this.execute = execute;
this.canExecute = canExecute;
}
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
this.CanExecuteChangedInternal += value;
}
remove
{
CommandManager.RequerySuggested -= value;
this.CanExecuteChangedInternal -= value;
}
}
public bool CanExecute(object parameter)
{
return this.canExecute != null && this.canExecute(parameter);
}
public void Execute(object parameter)
{
this.execute(parameter);
}
public void OnCanExecuteChanged()
{
EventHandler handler = this.CanExecuteChangedInternal;
if (handler != null)
{
//DispatcherHelper.BeginInvokeOnUIThread(() => handler.Invoke(this, EventArgs.Empty));
handler.Invoke(this, EventArgs.Empty);
}
}
public void Destroy()
{
this.canExecute = _ => false;
this.execute = _ => { return; };
}
private static bool DefaultCanExecute(object parameter)
{
return true;
}
}
更新
我从DockPanel
切换到StackPanel
并将宽度设置为StatusBox
,而不是让LastChildFill
调整状态字段,这解决了我的问题。即使我在StatusBox
周围放置一个边框来查看它所呈现的位置,但它仍然给了我一些问题。
答案 0 :(得分:0)
根据是否可以执行命令,您无需明确绑定到SaveEnabled属性以使按钮被禁用/启用。 只需在RelayCommand的谓词中包含CanExecute逻辑,如果属性发生更改,请在命令上调用OnCanExecuteChanged以启用该按钮。
修改强>
像这样的东西
<Button Content="Save" x:Name="SaveBtn" Command="{Binding SaveCMD}" Padding="10,5" Margin= "10,0"/>
然后在你看来你应该能够使用它,它应该有效:
Private Sub CommandButton3_Click()
Dim VectorGrafica(26) As Integer
Dim cuenta As Integer
Dim ho As Integer
Dim ma As Integer
Dim ejex As Range
Dim numero As Integer
Dim ejey As Range
cuenta = 0
da = 1
If ComboBox3 = "" Or ComboBox4 = "" Then
MsgBox "No ha determinado el horizonte de tiempo", vbOKOnly, "Horizonte de tiempo"
End If
For s = 24 To 46
If Controls("CheckBox" & s) = True Then
cuenta = 1 + cuenta
VectorGrafica(da) = 1
da = da + 1
Else
VectorGrafica(da) = 0
da = da + 1
End If
Next s
If cuenta = 0 Then
MsgBox "No ha seleccionado ningún rubro", vbOKOnly, "Rubros"
End If
If cuenta > 0 Then
numero = 1
Select Case ComboBox3.ListIndex
Case 0
ma = 2
Case 1
ma = 3
Case 2
ma = 4
Case 3
ma = 5
Case 4
ma = 6
Case 5
ma = 7
Case 6
ma = 8
Case 7
ma = 9
Case 8
ma = 10
Case 9
ma = 11
Case 10
ma = 12
Case 11
ma = 13
End Select
Select Case ComboBox4.ListIndex
Case 0
ho = 2
Case 1
ho = 3
Case 2
pa = 4
Case 3
ho = 5
Case 4
ho = 6
Case 5
ho = 7
Case 6
ho = 8
Case 7
ho = 9
Case 8
ho = 10
Case 9
ho = 11
Case 10
ho = 12
Case 11
ho = 13
End Select
Dim hola As Range
ActiveSheet.Shapes.AddChart2(240, xlXYScatterSmooth).Select
For i = 1 To 25
If VectorGrafica(i) = 1 Then
ActiveChart.Activate
Set rangodenombre = Sheets(ComboBox6.Text).Cells(i + 1, 1)
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(numero).Name = rangodenombre
ActiveChart.SeriesCollection(numero).XValues =
_Sheets(ComboBox6.Text).Range(Cells(1, ma), Cells(1, ho))
ActiveChart.SeriesCollection(numero).Values =
Sheets(ComboBox6.Text).Range(Cells(i + 1, ma), Cells(1 + i, ho))***
numero = numero + 1
End If
Next i
End If
End Sub