zoukankan      html  css  js  c++  java
  • 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>
     
     
  • 相关阅读:
    手动安装vue-devtools
    redis随记
    JS时间格式化
    360自动抢票还不够,几行js代码设置无人值守
    HttpWebRequest请求返回非200的时候 HttpWebResponse怎么接受返回错误提示
    android发编译
    asprise-ocr-api-sample 高价收破解版64 32位
    (16)集合操作
    (15)字典操作
    (14)字符串
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1325420.html
Copyright © 2011-2022 走看看