2

我想制作一个单选按钮,这样当按钮获得焦点时,它就会被选中。我有一个我想要实现的例子:

<Style x:Key="RadioStyle" TargetType="RadioButton">
    <Setter Property="IsChecked" Value="{Binding MyBoolean, Mode=TwoWay}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="RadioButton">
                <Border Name="RadioBorder"
                        Margin="4">
                    <ContentPresenter/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter TargetName="RadioBorder" Property="Background" Value="Red"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsFocused" Value="True">
            <Setter Property="IsChecked" Value="True"/>
        </Trigger>
    </Style.Triggers>
</Style>

问题:

当视图模型中的 MyBoolean 发生更改时,单选按钮将被选中。当我单击或选择单选按钮时,MyBoolean 保持不变。如果我删除样式触发器,问题就会消失,但是我需要能够选择焦点。

4

1 回答 1

1

我已经重现了你的问题。但一切正常。

问题可能是GroupName单选按钮的属性没有定义。为两个单选按钮的 GroupName 指定相同的名称。

前任:

<RadioButton Content="No" GroupName="group1" />
<RadioButton Content="Yes" GroupName="group1" />

这是我的完整 XAML:

<Window x:Class="RadioButtonFocus.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        Title="MainWindow" Height="350" Width="525">

    <Grid Name="mainGrid">
        <StackPanel Orientation="Horizontal">

            <RadioButton Content="No" GroupName="group1" IsChecked="{Binding IsNoChecked}">

                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseEnter">
                        <i:InvokeCommandAction Command="{Binding CheckNoCommand}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>

            </RadioButton>

            <RadioButton Content="Yes" GroupName="group1" IsChecked="{Binding IsYesChecked}">

                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseEnter">
                        <i:InvokeCommandAction Command="{Binding CheckYesCommand}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>

            </RadioButton>

        </StackPanel>
    </Grid>
</Window>

视图模型

public class MainViewModel : ViewModelBase
{
    #region Declarations

    private bool isNoChecked;
    private bool isYesChecked;

    private ICommand checkNoCommand;
    private ICommand checkYesCommand;
    #endregion

    #region Properties

    /// <summary>
    /// Gets or sets a value indicating whether this instance is no checked.
    /// </summary>
    /// <value>
    ///     <c>true</c> if this instance is no checked; otherwise, <c>false</c>.
    /// </value>
    public bool IsNoChecked
    {
        get
        {
            return isNoChecked;
        }
        set
        {
            isNoChecked = value;
            NotifyPropertyChanged("IsNoChecked");
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this instance is yes checked.
    /// </summary>
    /// <value>
    ///     <c>true</c> if this instance is yes checked; otherwise, <c>false</c>.
    /// </value>
    public bool IsYesChecked
    {
        get
        {
            return isYesChecked;
        }
        set
        {
            isYesChecked = value;
            NotifyPropertyChanged("IsYesChecked");
        }
    }

    #endregion

    #region Commands

    /// <summary>
    /// Gets the check no command.
    /// </summary>
    /// <value>The check no command.</value>
    public ICommand CheckNoCommand
    {
        get
        {
            if (checkNoCommand == null)
            {
                checkNoCommand = new RelayCommand(param => this.CheckNo(),
                    null);
            }
            return checkNoCommand;
        }
    }

    /// <summary>
    /// Gets the check yes command.
    /// </summary>
    /// <value>The check yes command.</value>
    public ICommand CheckYesCommand
    {
        get
        {
            if (checkYesCommand == null)
            {
                checkYesCommand = new RelayCommand(param => this.CheckYes(),
                    null);
            }
            return checkYesCommand;
        }
    }

    #endregion

    #region Private Methods

    /// <summary>
    /// Changes the checked.
    /// </summary>
    private void CheckNo()
    {
        this.IsNoChecked = true;
    }

    /// <summary>
    /// Checks the yes.
    /// </summary>
    private void CheckYes()
    {
        this.IsYesChecked = true;
    }

    #endregion
}

注意:这里我使用了交互库。您必须安装可再发行组件才能运行此示例。你可以从这里下载

于 2013-03-08T05:13:51.590 回答