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、ラジオ ボタンのプロパティが定義されていない可能性があります。2 つのラジオ ボタンの 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 に答える