2014-01-06 42 views
0

我有一組2個單選按鈕,我需要知道用戶何時單擊其中一個按鈕。 默認情況下(創建頁面時)必須選擇這兩個單選按鈕之一。單選按鈕:用戶單擊和編程選擇的區別

爲什麼從代碼中選擇單選按鈕時會調用檢查回調?我如何區分它們?它使我困擾,因爲當用戶執行操作時,我必須執行一個Web請求,但是當我在初始化時從代碼中選擇它時,它不能執行任何請求。

private void RadioButton_Checked(object sender, RoutedEventArgs e) 
{ 
    Search(ContractTextBox.Text, true); 
} 

<ItemsControl ItemsSource="{Binding SelectedMainSearchItem.SubLevels}"> 
            <ItemsControl.ItemTemplate> 
             <DataTemplate> 
              <Grid Margin="10"> 
               <RadioButton Content="{Binding Name}" 
                GroupName="ExclusiveGroupL3" 
                IsChecked="{Binding IsSelected, Mode=TwoWay}" 
                Checked="RadioButton_Checked"   
                FontSize="18"/> 
              </Grid> 
             </DataTemplate> 
            </ItemsControl.ItemTemplate 

回答

0

通常的方式來處理這樣的情況是定義一個bool屬性作爲「標誌」的使用方法:

private bool isUserSelection = true; 

... 

private void RadioButton_Checked(object sender, RoutedEventArgs e) 
{ 
    if (isUserSelection) Search(ContractTextBox.Text, true); 
} 

而且當你設定的CheckBox值編程,設置標誌到false

isUserSelection = false; 
SetCheckBoxValue(true); 
isUserSelection = true;