wpf radiobuttonlist实现
2016-06-24 16:30
344 查看
1、创建类:MulitBooleanConverter
2、界面RadioButton
3、后台选中事件
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()); }
相关文章推荐
- JavaScript中获取Radio被选中的值
- JavaScript通过this变量快速找出用户选中radio按钮的方法
- js实现的简单radio背景颜色选择器代码
- javascript radio list的实现细节(多浏览器兼容)
- 利用JavaScript更改input中radio和checkbox样式
- jquery获取radio值(单选组radio)
- 解决jquery实现的radio重新选中的问题
- Jquery判断radio、selelct、checkbox是否选中及获取选中值方法总结
- jQuery实现自定义checkbox和radio样式
- JQuery判断radio是否选中并获取选中值的示例代码
- jQuery获取radio选中项的值实例
- 三种取消选中单选框radio的方法
- 新老版本juqery获取radio对象的方法
- jquery获取input表单值的代码
- 利用jquery操作Radio方法小结
- js实现表单Radio切换效果的方法
- select、radio表单回显功能实现避免使用jquery载入赋值
- 为radio类型的INPUT添加客户端脚本(附加实现JS来禁用onClick事件思路代码)
- php radio 单选框获取与保持值的实现代码
- php select,radio和checkbox默认选择的实现方法