mar*_*n87 1 wpf master-detail radio-button
我有一个主从WPF应用程序。“ master”是一个数据网格,“ detail”是两个单选按钮。基于行选择,将在“详细信息”部分中选中单选按钮。
我使用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>
Run Code Online (Sandbox Code Playgroud)
在视图模型中:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
对于datagrid中的前几个选择,我的实现效果很好。突然之间,我的单选按钮都没有被选中。
我不知道为什么会这样,欢迎提出任何建议。
提前致谢。
搜索有关绑定多个RadioButton的问题-那里有足够的投诉。基本上,绑定不会收到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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3689 次 |
| 最近记录: |