1

マスター/詳細wpfアプリケーションがあります。「マスター」はデータグリッドであり、「詳細」は2つのラジオボタンです。行の選択に基づいて、「詳細」セクションでラジオボタンがチェックされます。

inttobooleanコンバーターを使用して、次の方法でラジオボタンをバインドしています。xaml:

<StackPanel Margin="2">
  <RadioButton Margin="0,0,0,5" Content="In Detail" IsChecked="{Binding Path=itemselect.OutputType, Converter ={StaticResource radtointOTSB}, ConverterParameter= 0}"/>
  <RadioButton Content="In Breif" IsChecked="{Binding Path=itemselect.OutputType, Converter ={StaticResource radtointOTSB}, ConverterParameter= 1}"/>
</StackPanel>

ビューモデルの場合:

public class radtointOTSB : IValueConverter
{
    object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int OTint = Convert.ToInt32(value);
        if (OTint == int.Parse(parameter.ToString()))
            return true;
        else
            return false;
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return parameter;
    }
}

私の実装は、データグリッドの最初のいくつかの選択でうまく機能します。そして突然、私のラジオボタンのどちらも選択されていません。

なぜそれが起こるのか私にはわかりません、どんな提案も歓迎します。

前もって感謝します。

4

1 に答える 1

5

複数のラジオボタンのバインドに関する問題を検索します-そこには十分な苦情があります。基本的に、バインディングはDependency Property..etcなどに渡されないため、Falseの値を受け取りません。

チェックボックスのIsChecked値を強制的に更新するため、通常のRadioButtonの代わりに次のクラスを使用してIsCheckedExtにバインドしてみてください。

public class RadioButtonExtended : RadioButton
{
    public static readonly DependencyProperty IsCheckedExtProperty =
        DependencyProperty.Register("IsCheckedExt", typeof(bool?), typeof(RadioButtonExtended),
                                    new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, IsCheckedRealChanged));

    private static bool _isChanging;

    public RadioButtonExtended ()
    {
        Checked += RadioButtonExtendedChecked;
        Unchecked += RadioButtonExtendedUnchecked;
    }

    public bool? IsCheckedExt
    {
        get { return (bool?)GetValue(IsCheckedExtProperty); }
        set { SetValue(IsCheckedExtProperty, value); }
    }

    public static void IsCheckedRealChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        _isChanging = true;
        ((RadioButtonExtended)d).IsChecked = (bool)e.NewValue;
        _isChanging = false;
    }

    private void RadioButtonExtendedChecked(object sender, RoutedEventArgs e)
    {
        if (!_isChanging)
            IsCheckedExt = true;
    }

    private void RadioButtonExtendedUnchecked(object sender, RoutedEventArgs e)
    {
        if (!_isChanging)
            IsCheckedExt = false;
    }
}
于 2012-05-04T21:18:11.317 に答える