3

nullabletoboolconverters も使用したのと同じグループ名で、2 つのラジオボタンを xaml フォームと IsChecked プロパティにバインドできません。ただし、ラジオボタンの ischecked プロパティは私のコードでは変更されません (2 つ目のラジオボタンの後に最初のラジオボタンを押すと、ブレークポイントにはまったくヒットしません)。 radiobuttons プロパティに基づくフォーム上の他のパネルの可視性。

以下は私のxamlコードで、後でラジオボタンプロパティのviewmodel.csコードです:

   <RadiobuttonSelectedConverter x:Key="CheckedSelection"/>// declaring my converter class in my resource dictionary.

  <StackPanel Orientation="Horizontal" Grid.Column="0" Grid.Row="3" Margin="10,5,0,0">
        <RadioButton GroupName="RadiosGroup" IsChecked="{Binding IsRadioButton1,Mode=TwoWay,
             Converter={StaticResource CheckedSelection}, ConverterParameter=true}">
                    First</RadioButton>
        <RadioButton GroupName="RadiosGroup" 
                     Margin="40,0,0,0" IsChecked="{Binding IsRadioButton2,Mode=TwoWay, 
            Converter={StaticResource CheckedSelection}, ConverterParameter=true}">Second</RadioButton>
    </StackPanel>



    private bool _isRadioButton1;

    public bool IsRadioButton1
    {
        get
        {
            return _isRadioButton1;
        }
        set
        {
            if _isRadioButton1!= value)
            {
                _isRadioButton1= value;

                IsRadioButton2= false;
                OnPropertyChanged("IsRadioButton1");

            }
        }
    }


    private bool _isRadioButton2;

    public bool IsRadioButton2
    {
        get
        {
            return _isRadioButton2;
        }
        set
        {
            if (_isRadioButton2 != value)
            {
                _isRadioButton2 = value;

              IsRadioButton1= false;
              OnPropertyChanged("IsRadioButton2");

            }
        }
    }

以下は私のコンバーターコードです:

  [ValueConversion(typeof(bool?), typeof(bool))]
public class RadiobuttonSelectedConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool param = bool.Parse(parameter.ToString());
        if (value == null)
        {
            return false;
        }
        else
        {
            return !((bool)value ^ param);
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool param = bool.Parse(parameter.ToString());
        return !((bool)value ^ param);
    }
}

誰かが私の問題を解決するのを手伝ってください..

4

2 に答える 2

6

個人的にはRadioButtons、このように関連付けられたコードはまったくありません。必要な選択動作は、に使用されるものと同じであるため、各アイテムに使用するスタイルのを使用するのがListBox最も簡単です。ListBoxRadioButtons

通常、コードビハインドには次のものが含まれます

  • ObservableCollection<string> Options
  • string SelectedOption

そして私はこのスタイルをListBox:に使用します

<Style x:Key="RadioButtonListBoxStyle" TargetType="{x:Type ListBox}">
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle" />
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type ListBoxItem}" >
                <Setter Property="Margin" Value="2, 2, 2, 0" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border Background="Transparent">
                                <RadioButton
                                    Content="{TemplateBinding ContentPresenter.Content}" VerticalAlignment="Center"
                                    IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/>

                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

スタイルは次のように適用されます。

<ListBox ItemsSource="{Binding Options}"
         SelectedValue="{Binding SelectedOption}"
         Style="{StaticResource RadioButtonListBoxStyle}" />

Stringコレクションにaの代わりに、などの他の何かを使用ChildViewModelして、現在のアイテムに基づいて関連するビューを設定することもできます。つまり、どちらVisibilityの関連​​するパネルのを気にする必要もありませんViewModel

<DockPanel>
    <ListBox ItemsSource="{Binding OptionViewModels}"
             SelectedValue="{Binding SelectedViewModel}"
             Style="{StaticResource RadioButtonListBoxStyle}"
             DockPanel.Dock="Left">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding DisplayName}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

    <ContentControl Content="{Binding SelectedViewModel}" />
</DockPanel>

しかし、あなたの実際の問題に関しては、私はそれが正しく動作していないかもしれない3つの理由を考えることができます。

1つ目は、 Jayの回答で概説されIsChecked2ています。のセッターでfalseに設定しIsChecked1、セッターでfalseにIsChecked2設定IsChecked1しているため、最終結果は両方の値がfalseになります。

2つ目は、コンバーターに問題がある可能性があることです。コンバーターがなくても正常に動作しているとのコメントでしたので、問題の一部かもしれません。

そして最後に、グループ化を変更すると、古いアイテムと新しく選択されたアイテムのRadioButtonsがトリガーされ、これら2つがどこかで交差する可能性があると思います。IsOptionA.IsSelected = falseIsOptionB.IsSelected = true

于 2012-08-27T14:20:11.643 に答える
1

ここでいくつかの問題があります。

  1. コンバーターは必要ありません。タイプ のMakeIsRadioButton1IsRadioButton2プロパティbool?、および で十分です。または、トライステートが適用されないかのようTwoWay Bindingにそのままにしておきます。bool
  2. セッターのロジックが正しくないようです。どちらの場合も、もう一方の値を に設定しているためRadioButtonfalseをに設定すると、セッターが を呼び出し、そのセッターが を呼び出します。どちらも になります。IsRadioButton1trueIsRadioButton2trueIsRadioButton = falseIsRadioButton2 = falsefalse

あなたはおそらくこれを読んでほしいif(value) IsRadioButton2 = false;


編集

実際、私が思い出したように、aのようにプロパティRadioButtonにバインドされることを意図したものではありません。すべてのs を 1 つのプロパティにバインドし、 aと aを使用してプロパティを設定すると思います。例を見つけてすぐに投稿します。boolCheckBoxRadioButtonConverter ConverterParameter


さて、これは純粋にバインドで動作する派生RadioButtonクラスを使用した 1 つのソリューションです。

関連する SO の質問と回答は次のとおりです: MVVM: ラジオ ボタンをビュー モデルにバインドしますか?

于 2012-08-27T13:25:52.333 に答える