WPF控件使用之两个RadioButton-绑定一个bool property

通常RadioButton用于在多个选项中选择一个,如果选项比较多,那么用ComboBox会比较适合,如果选项不是太多,那么用RadioButton比较好。

这里将两个radiobutton绑定到viewmodel里的一个bool属性中,绑定的模式是twoway的。效果是这样的:

其中高限制和低限制分别对应两个组的radiobutton,每一个组的两个radiobutton都绑定同一个bool变量,这里我发现如果只是单纯绑定而不设groupname,会默认将全部他们划分为一个组内,这样会很出现堆栈溢出的问题,因为四个radiobutton中任意触发其中一个,都会导致其他三个变化,而三个中有两个的关系是互斥的,这样导致了两个一直互相通知进入死循环。

另外,在“固定”和“可变”的check下,会动态改变右边需要设置的内容,这里我用了ContentControl 控件来动态选择需要呈现的内容。

前端代码如下:

 1  <UserControl.Resources>
 2         <convert:BoolValueConverter x:Key="BoolConvert"/>
 3                <DataTemplate x:Key="HighFixPanel">
 4             <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" >
 5                 <Label Content="固定值:" Height="28" Name="label25" />
 6                 <TextBox Height="23"
 7                          Name="textBox20" 
 8                          HorizontalAlignment="Left" 
 9                          Width="108">
10                 </TextBox>
11             </StackPanel>
12         </DataTemplate>
13         <DataTemplate  x:Key="HighVaraiblePanel">
14             <StackPanel Orientation="Horizontal"  HorizontalAlignment="Left">
15                 <Label>OPC Item Name :</Label>
16                 <TextBox Width="150" Height="26"></TextBox>
17                 <Button Width="30" Height="26">B</Button>
18                 <Button Width="30" Height="26">T</Button>
19             </StackPanel>
20         </DataTemplate>
21         <DataTemplate  x:Key="LowFixPanel" >
22             <StackPanel Orientation="Horizontal" Width="200"  HorizontalAlignment="Left">
23                 <Label  Content="固定值:" Height="28" Name="label27" />
24                 <TextBox  Height="23" Name="textBox21" 
25                          HorizontalAlignment="Left" 
26                          Width="108" >
27                 </TextBox>
28             </StackPanel>
29         </DataTemplate>
30         <DataTemplate  x:Key="LowVaraiblePanel">
31             <StackPanel Orientation="Horizontal" Width="500"  HorizontalAlignment="Left">
32                 <Label>OPC Item Name :</Label>
33                 <TextBox Width="150" Height="26"></TextBox>
34                 <Button Width="30" Height="26">B</Button>
35                 <Button Width="30" Height="26">T</Button>
36             </StackPanel>
37         </DataTemplate>
38 
39     </UserControl.Resources>
 <Label Grid.Column="1" Content="固定" Height="28" Name="label20" HorizontalAlignment="Right" 
                       Margin="0,5" Width="38" />
                <Label Grid.Row="0" Grid.Column="2" Content="可变" Margin="12,5" Height="28" Name="label21" />
                <Label Grid.Row="1" Content="高限制" Height="28" Name="label24" HorizontalAlignment="Right"
                       Margin="0,1,0,0" VerticalAlignment="Top" />

                <RadioButton Grid.Row="1" Grid.Column="1" 
                             Height="16" Name="radHighFix" 
                             HorizontalAlignment="Center" 
                             IsChecked="{Binding Path=LoopSpan.HighLimitIsFix}"/>

                <RadioButton  Grid.Row="1" Grid.Column="2" Height="16"
                              Name="radioButton2" HorizontalAlignment="Center" 
                              IsChecked="{Binding Path=LoopSpan.HighLimitIsFix,Converter={StaticResource BoolConvert}}"/>

                <ContentControl Grid.Column="3" Grid.Row="1" Grid.ColumnSpan="3">
                    <ContentControl.Style>
                        <Style TargetType="ContentControl">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=radHighFix, Path=IsChecked}" Value="True">
                                    <Setter Property="ContentTemplate" Value="{StaticResource HighFixPanel}" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding ElementName=radHighFix, Path=IsChecked}" Value="False">
                                    <Setter Property="ContentTemplate" Value="{StaticResource HighVaraiblePanel}" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ContentControl.Style>
                </ContentControl>

                <Label Grid.Row="2" Grid.Column="0" Content="低限制" 
                       Height="28" Name="label26" HorizontalAlignment="Right"/>

                <RadioButton Grid.Row="2" Grid.Column="1"
                             Height="16" Name="radLowFix" GroupName="2"
                             HorizontalAlignment="Center"  
                             IsChecked="{Binding Path=LoopSpan.LowLimitIsFix}"/>

                <RadioButton Grid.Row="2" Grid.Column="2" Height="16" GroupName="2"
                             Name="radioButton4" HorizontalAlignment="Center"
                            IsChecked="{Binding Path=LoopSpan.LowLimitIsFix,Converter={StaticResource BoolConvert}}"/>

                <ContentControl  Grid.Column="3" Grid.Row="2" Grid.ColumnSpan="3">
                    <ContentControl.Style>
                        <Style TargetType="ContentControl">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding LoopSpan.LowLimitIsFix}" Value="True">
                                    <Setter Property="ContentTemplate" Value="{StaticResource LowFixPanel}" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding LoopSpan.LowLimitIsFix}" Value="False">
                                    <Setter Property="ContentTemplate" Value="{StaticResource LowVaraiblePanel}" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ContentControl.Style>
                </ContentControl>

其中一个组里绑定的后端属性是:

 1  bool _HighLimitIsFix;
 2 
 3         public bool HighLimitIsFix
 4         {
 5             get { return _HighLimitIsFix; }
 6             set { _HighLimitIsFix = value; 
 7                 OnPropertyChanged("HighLimitIsFix"); }
 8         }
 9         bool _lowLimitIsFix;
10 
11         public bool LowLimitIsFix
12         {
13             get { return _lowLimitIsFix; }
14             set { _lowLimitIsFix = value; 
15                 OnPropertyChanged("LowLimitIsFix"); }
16         }
17         double? _HighLimitFixValue;
18 
19         public double? HighLimitFixValue
20         {
21             get { return _HighLimitFixValue; }
22             set { _HighLimitFixValue = value; 
23                 OnPropertyChanged("HighLimitFixValue"); }
24         }
25         double? _lowLimitFixValue;
26 
27         public double? LowLimitFixValue
28         {
29             get { return _lowLimitFixValue; }
30             set { _lowLimitFixValue = value; 
31                 OnPropertyChanged("LowLimitFixValue"); }
32         }
33 
34         private string highLimitOpcItemName;
35 
36         public string HighLimitOpcItemName
37         {
38             get { return highLimitOpcItemName; }
39             set
40             {
41                 highLimitOpcItemName = value;
42                 OnPropertyChanged("HighLimitOpcItemName");
43             }
44         }
45 
46         private string lowLimitOpcItemName;
47 
48         public string LowLimitOpcItemName
49         {
50             get { return lowLimitOpcItemName; }
51             set
52             {
53                 lowLimitOpcItemName = value;
54                 OnPropertyChanged("LowLimitOpcItemName");
55             }
56         }

然后,两个radiobutton绑定一个bool值,在相反的那个需要一个转换器来转为相反的值

 1  public class BoolValueConverter : IValueConverter
 2     {
 3         //源转目标
 4         public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
 5         {
 6             if (value is bool)
 7             {
 8                 return !(bool)value;
 9             }
10             return value;
11         }
12         //目标转源
13         public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
14         {
15             if (value is bool)
16             {
17                 return !(bool)value;
18             }
19             return value;
20         }
21     }


 

posted @ 2016-01-15 10:24  hmc_up  阅读(2635)  评论(0)    收藏  举报