wpf radiobuttonlist实现

1、创建类:MulitBooleanConverter

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace ScreenLock
{
    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

    }
}

2、界面RadioButton

<Window x:Class="ScreenLock.RadioButton"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="RadioButton" Height="300" Width="300"
        xmlns:local="clr-namespace:ScreenLock"
        >
    <Window.Resources>
        <local:MulitBooleanConverter x:Key="boolConverter"></local:MulitBooleanConverter>
        <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>
    </Window.Resources>
    <Grid>
        <ListBox Style="{StaticResource HorizontalRadioButtonListStyle}" x:Name="list1" SelectionChanged="list1_SelectionChanged">
            <ListBox.Items>
                <ListBoxItem  IsSelected="{Binding xxx,Converter={StaticResource boolConverter},ConverterParameter=0}" Content="在线" ></ListBoxItem>
                <ListBoxItem IsSelected="{Binding xxx,Converter={StaticResource boolConverter},ConverterParameter=1}" Content="离线"></ListBoxItem>
                <ListBoxItem IsSelected="{Binding xxx,Converter={StaticResource boolConverter},ConverterParameter=0}" Content="离线llll"></ListBoxItem>
            </ListBox.Items>
        </ListBox>
    </Grid>
</Window>

3、后台选中事件

  private void list1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MessageBox.Show(((ListBoxItem)list1.SelectedItem).Content.ToString());
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值