首页 > 解决方案 > 与单选按钮组选择相关的元素可见性导致组冻结

问题描述

我有一个数据绑定 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; }
        }

标签: wpfradio-button

解决方案


问题出在 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 按钮。为什么这应该锁定单选按钮和取消按钮仍然是一个谜。


推荐阅读