您的位置:首页 > 其它

wpf radiobuttonlist实现

2016-06-24 16:30 344 查看
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());
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  radio