1

我有一个主从 wpf 应用程序。“master”是一个 datagrid ,“detail”是两个单选按钮。根据行选择,在“详细信息”部分中选中单选按钮。

我正在使用 inttoboolean 转换器通过以下方式绑定我的单选按钮。xml:

<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

搜索绑定多个 RadioButtons 的问题 - 那里有足够多的投诉。基本上绑定不会收到 False 的值,因为它没有被传递给 Dependency Property..etc 等

尝试使用以下类而不是常规 RadioButton,绑定到 IsCheckedExt,因为它会强制更新复选框的 IsChecked 值。

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 回答