确保一次只检查一个CheckBox

时间:2015-11-10 11:15:58

标签: c# wpf checkbox mvvm

我有WPF数据网格,其中包含两行复选框:

  <Grid>
     <Grid.ColumnDefinitions>
       <ColumnDefinition Width="*" />
       <ColumnDefinition Width="3*"/>
     </Grid.ColumnDefinitions>
     <DataGrid VerticalAlignment="Stretch" 
               HorizontalAlignment="Stretch"
               Name="dsGrid"
               AutoGenerateColumns="False" Grid.Column="1" >
       <DataGrid.Columns>     
         <DataGridTextColumn Header="Properties Name" MinWidth="150" Binding="{Binding Propertie}" />     
                 <DataGridTemplateColumn Header=" Server " MinWidth="150" >
                     <DataGridTemplateColumn.CellTemplate>
                         <DataTemplate>
                             <StackPanel>
                                 <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                 <TextBlock Name="PropertiesTextBlock" 
                                            HorizontalAlignment="Right" 
                                            VerticalAlignment="Center" 
                                            Text="{Binding Server}"
                                            Grid.Column="1"/>
                                 <CheckBox IsChecked="{Binding                                       Path=IsServerSelected, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                           Checked="Server_Checked" 
                                           VerticalAlignment="Center"
                                           HorizontalAlignment="Left" 
                                           Visibility="{Binding Equal,
                                           Converter={StaticResource BoolToVisibility},FallbackValue=Hidden}" 
                                           Grid.Column="0"/>
                                </Grid>     
                             </StackPanel>
                         </DataTemplate>
                     </DataGridTemplateColumn.CellTemplate>
                 </DataGridTemplateColumn>     
                 <DataGridTemplateColumn Header=" Local " MinWidth="150">
                     <DataGridTemplateColumn.CellTemplate>
                         <DataTemplate>
                             <StackPanel>
                                 <Grid>
                                     <Grid.ColumnDefinitions>
                                         <ColumnDefinition Width="*" />
                                         <ColumnDefinition Width="*"/>
                                     </Grid.ColumnDefinitions>
                                     <TextBlock Name="PropertiesTextBlock" 
                                                HorizontalAlignment="Right" 
                                                Text="{Binding Local}" 
                                                Grid.Column="1"/>
                                     <CheckBox IsChecked="{Binding Path=IsLocalSelected, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                               Checked="Local_Checked" HorizontalAlignment="Left"
                                               Visibility="{Binding Equal, Converter={StaticResource BoolToVisibility},FallbackValue=Hidden}"       Grid.Column="0"></CheckBox>
                                 </Grid>
                             </StackPanel>
                         </DataTemplate>
                     </DataGridTemplateColumn.CellTemplate>
                 </DataGridTemplateColumn>     
             </DataGrid.Columns>
         </DataGrid>
         <ListBox HorizontalAlignment="Stretch"
                  VerticalAlignment="Stretch"  Name="Entities_List"
                  SelectionChanged="Entities_List_SelectionChanged"
                  SelectionMode="Single"
                  Grid.Column="0"/>
         <Button Content="Merge"
                 Name="Merge_Button"
                 Click="Merge_Button_Click"
                 HorizontalAlignment="Stretch"
                 VerticalAlignment="Bottom"
                 Grid.Column="0"  />
     </Grid>

DataContext ViewModel:

public string Server { get; set; }
public string Local { get; set; }    
private bool _pIsServerSelected;
private bool _pIsLocalSelected;

public bool IsLocalSelected
{
    get
    {
        return _pIsLocalSelected;
    }
    set
    {
        _pIsLocalSelected = value;
        OnPropertyChanged("IsSelected");
    }
}

public bool IsServerSelected
{
    get
    {
        return _pIsServerSelected;
    }
    set
    {
        _pIsServerSelected = value;
        OnPropertyChanged("IsSelected");
    }
}  

我的需求是确保一次只检查一个复选框。 因此,如果ServerCheckBox被检查,我想取消选中LocalCheckBox。

1 个答案:

答案 0 :(得分:0)

你几乎得到了你需要的一切:

    public bool IsLocalSelected
    {
        get
        {
            return pIsLocalSelected;
        }
        set //LocalSelected is Set: -> UnSet Server
        {
            pIsLocalSelected = value;
            OnPropertyChanged("IsLocalSelected");
            IsServerSelected = false;
            OnPropertyChanged("IsServerSelected");
        }
    }

    public bool IsServerSelected
    {
        get
        {
            return pIsServerSelected;
        }
        set //ServerSelected is Set: -> UnSet Local
        {
            pIsServerSelected = value;
            OnPropertyChanged("IsServerSelected");
            IsLocalSelected = false;
            OnPropertyChanged("IsLocalSelected");
        }
    }

这样就可以了。

  

但这将创建一个无限循环......

为了解决这个问题,我建议使用RadioButton代替CheckBox。 如果选择“本地”,则将取消选择服务器,反之亦然。