wpf - 与单选按钮组选择相关的元素可见性导致组冻结
问题描述
我有一个数据绑定 RadioButton 组,其中一个 RadioButton (Replace) 控制另一个元素的可见性。当我选择替换按钮时,组变得无响应。有趣的是,表单取消按钮也是如此。但是,作为命令实现的 OK 按钮继续起作用(Cancel 按钮只是实现为 IsCancel="true")。
<Border x:Name="pnlOptions" Margin="10" Background="AliceBlue" BorderThickness="2" CornerRadius="25">
<GroupBox TextElement.FontSize="14" TextElement.FontWeight="Bold" >
<StackPanel Orientation="Vertical">
<RadioButton GroupName="grpOptions" Margin="10,10,0,0" IsChecked="{Binding Path=SegmentSplitOption, Converter={StaticResource EnumRadioButtonConverter}, ConverterParameter={x:Static model:eSegmentSplitOption.Replace},Mode=TwoWay}">Replace original segment.</local:RadioButton>
<RadioButton GroupName="grpOptions" Margin="10,10,0,0" IsChecked="{Binding Path=SegmentSplitOption, Converter={StaticResource EnumRadioButtonConverter}, ConverterParameter={x:Static model:eSegmentSplitOption.Start},Mode=TwoWay}">Place at Start.</local:RadioButton>
<RadioButton GroupName="grpOptions" Margin="10,10,0,0" IsChecked="{Binding Path=SegmentSplitOption, Converter={StaticResource EnumRadioButtonConverter}, ConverterParameter={x:Static model:eSegmentSplitOption.Finish},Mode=TwoWay}">Place at End.</local:RadioButton>
<RadioButton GroupName="grpOptions" Margin="10,10,0,10" IsChecked="{Binding Path=SegmentSplitOption, Converter={StaticResource EnumRadioButtonConverter}, ConverterParameter={x:Static model:eSegmentSplitOption.Mid},Mode=TwoWay}">Place at Drop Location.</local:RadioButton>
</StackPanel>
</GroupBox>
</Border>
<StackPanel Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" Orientation="Horizontal" Visibility="{Binding DurationVisibility}">
<Label>Duration</Label>
<local:TextBox Grid.Row="2" Grid.Column="1" x:Name="txtDuration" Width="80" HorizontalAlignment="Left" VerticalAlignment="Bottom" FontSize="14"
Text="{Binding Path=DurationMinutes, Converter={StaticResource DecimalHoursConverter}, Mode=TwoWay}" Style="{StaticResource GeneralTextBoxStyle}"/>
<Label Grid.Row="2" Grid.Column="2">(hh.dd)</Label>
</StackPanel>
private eSegmentSplitOption _SegmentSplitOption;
public eSegmentSplitOption SegmentSplitOption
{
get { return _SegmentSplitOption; }
set
{
if (_SegmentSplitOption != value)
{
_SegmentSplitOption = value;
NotifyPropertyChanged("SegmentSplitOption");
NotifyPropertyChanged("DurationVisibility");
}
}
}
public Visibility DurationVisibility
{
get { return _SegmentSplitOption == eSegmentSplitOption.Replace || _SegmentSplitOption == eSegmentSplitOption.NotSet ? Visibility.Collapsed : Visibility.Visible; }
}
解决方案
问题出在 CmdOK_CanExecute 函数的其他地方,该函数具有以下代码:
e.CanExecute = _SegmentSplitOption == eSegmentSplitOption.Replace || _DurationMinutes.HasValue && _DurationMinutes.Value > 0;
if (wPrevCanExecute != e.CanExecute && e.CanExecute)
{
Application.Current.Dispatcher.Invoke(new Action(() =>
{
btnOK.Focus();
}));
}
目的是在命令能够执行时将焦点设置为 OK 按钮。为什么这应该锁定单选按钮和取消按钮仍然是一个谜。
推荐阅读
- python - 使用 QThread 作为接收者和发送者
- javascript - 如何将多个函数值返回给多个全局变量?
- eclipse - Groovy:语义分析期间的一般错误:java.lang.NoSuchMethodError:
- ios - 从 ZStack 的背景中隐藏图像,直到它们被带到前景
- php - Symfony 树枝模板未在捆绑包中连接
- python - Python - 如何将选定的文本复制到Windows中的剪贴板
- sql - BigQuery 返回数值类型
- godot - 有没有办法限制角色可以移动的瓷砖数量?
- powershell - 如何将批处理变量发送到 powershell 命令
- python - 在python中查找每5个元素的累积和的函数