WPF and SL RadioButtonList Tip

      在以下情境下.使用数据绑定分离UI与后端Model,有两个RadioButton,用于选择True or False(如果用CheckBox则就没这么多复杂的问题了).

实现步骤如下,

(1)用ListBox定义一个RadioButton模板

<!--for RadioButton ListBox-->
<Style x:Key="HorizontalRadioButtonListStyle" TargetType="ListBox">
    <Style.Resources>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Grid Margin="2">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>
                            <RadioButton IsChecked="{Binding IsSelected,  
                                RelativeSource={RelativeSource TemplatedParent},  
                                Mode=TwoWay}" />
                            <ContentPresenter Grid.Column="1" Margin="2,0,0,0" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Style.Resources>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal"  />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Background" Value="Transparent" />
</Style>

(2)定义类型转换器(以传入的参数进行判定)

 

public class MulitBooleanConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int serviceParameter =int.Parse(parameter.ToString());
        bool isOnservice=(bool)value;
        bool result = false;
        switch (serviceParameter)
        {
            case 0:
                 result = true && isOnservice;
                break;
            case 1:
                
                result = !isOnservice;
                break;
            
        }
        return result;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int serviceParameter = int.Parse(parameter.ToString());
        bool isOnservice = (bool)value;
        bool result = false;
        switch (serviceParameter)
        {
            case 0:
                result = true && isOnservice;
                break;
            case 1:
                result = false && isOnservice;
                break;

        }
        return value;
    }
    #endregion
}

(3)使用如下(同时双向绑定一个属性xxx)

<ListBox Style="{StaticResource HorizontalRadioButtonListStyle}">
    <ListBox.Items>
        <ListBoxItem IsSelected="{Binding xxx,Converter={StaticResource boolConverter},ConverterParameter=0}">在线</ListBoxItem>
        <ListBoxItem IsSelected="{Binding xxx,Converter={StaticResource boolConverter},ConverterParameter=1}">离线</ListBoxItem>
    </ListBox.Items>
</ListBox>
 
 
原文地址:https://www.cnblogs.com/Clingingboy/p/1325420.html