2012-10-29 45 views
0

我是C++開發人員,最近轉移到C#。我在我的wpf應用程序中使用MVVM模式。我正在研究動態生成單選按鈕。那麼需求很簡單,我需要生成24個Radibuttons,這樣一次只能檢查一個單選按鈕。這裏是代碼:未能生成具有不同「內容」的動態集合的RadioButton

XAML:

<Grid Grid.Row="1"> 
     <GroupBox Header="Daughter Cards" > 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition />       
        <ColumnDefinition Width="220" /> 
       </Grid.ColumnDefinitions> 

       <Grid Grid.Column="0"> 
        <RadioButton Content="{Binding SlotButtons}" Name="SLotButtons" /> 
       </Grid> 
      </Grid> 
     </GroupBox> 
</Grid> 

Grid.Column="0"我要生成24個單選按鈕正如我上面所討論。

視圖模型:

// Description of SlotButtons 
    private string _SlotButtons; 
    public string SlotButtons 
    { 
     get 
     { 
      return _SlotButtons; 
     } 

     set 
     { 
      _SlotButtons = value; 
      OnPropertyChanged("SlotButtons"); 
     } 
    } 

//For RadioButton Click 
private ICommand mSlotCommand; 
    public ICommand SlotCommand 
    { 
     get 
     { 
      if (mSlotCommand == null) 
       mSlotCommand = new DelegateCommand(new Action(mSlotCommandExecuted), new Func<bool>(mSlotCommandCanExecute)); 

      return mSlotCommand; 
     } 
     set 
     { 
      mSlotCommand = value; 
     } 
    } 

    public bool mSlotCommandCanExecute() 
    { 
     return true; 
    } 

    public void mSlotCommandExecuted() 
    { 
     // Logic to implement on a specific radiobutton click using Index 
    } 

我曾在我的C++應用程序做到了這一點,如下所示:

for(slot = 0; slot < 24; slot++) 
{ 
    m_slotButton[slot] = new ToggleButton(String(int(slot)) + String(": None")); 
    m_slotButton[slot]->addButtonListener(this); // make this panel grab the button press  
    addAndMakeVisible(m_slotButton[slot]); 
} 

現在這就是我想要實現:

  1. 生成24 RadioButtons,內容從Content = 0: None23: None
  2. 應該以這樣一種方式生成輻射單元,即將行分成3列,並在每列中垂直添加8個單選按鈕。
  3. 在任何時候,只有一個單選按鈕必須被選中,而其他選項不能被選中。只有一個點擊命令可以在各個索引的幫助下處理所有按鈕。

請幫助:)

+1

如果您想使用MVVM,則不要手動創建RadioButton。您使用帶有包含RadioButton的ItemTemplate的ItemsControl,並將Items列表綁定到ItemsSource屬性 – Niki

+0

並獲取8 * 3網格佈局,您可以在ItemControl的ItemsPanel模板中使用UniformGrid – Niki

+0

@nikie:是I已經實施過一次。但是我在那裏發現了一個問題:當我點擊它們時,所有的單選按鈕都會被檢查,即一次必須檢查一個單選按鈕:) – StonedJesus

回答

2

你不需要你的SlotCommand。相反,只需將每個單選按鈕的IsChecked屬性綁定到視圖模型上的布爾屬性即可。

如: XAML:

<RadioButton Content="{Binding SlotButtons}" Margin="0,10,0,0" IsChecked="{Binding IsChecked}" GroupName="SlotGroup" Height="15" Width="80" HorizontalAlignment="Center" VerticalAlignment="Center"/> 

視圖模型:

public class SlotViewModel : ViewModelBase 
{ 
    private _isChecked; 

    public bool IsChecked 
    { 
     get { return _isChecked; } 
     set { _isChecked = value; NotifyPropertyChanged("IsChecked"); } 
    } 
} 

如果你想在當它被選中某種方式作出反應,只是做在IsChecked二傳手。

0

這裏是我如何實現它:

XAML:

<Grid Grid.Column="0"> 
        <ItemsControl ItemsSource="{Binding SlotChildren}"> 
         <ItemsControl.ItemsPanel> 
          <ItemsPanelTemplate> 
           <UniformGrid Columns="3" Rows="8" /> 
          </ItemsPanelTemplate> 
         </ItemsControl.ItemsPanel> 

         <ItemsControl.ItemTemplate> 
          <DataTemplate> 
           <RadioButton Content="{Binding SlotButtons}" Margin="0,10,0,0" Command="{Binding SlotCommand}" Height="15" Width="80" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
          </DataTemplate> 
         </ItemsControl.ItemTemplate> 
        </ItemsControl> 
       </Grid>  

ViewModel類:

public ObservableCollection<EEPROMSlotViewModel> SlotChildren { get; set; } 

SlotChildren = new ObservableCollection<EEPROMSlotViewModel>(); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "0 : None", ID = 0 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "1 : None", ID = 1 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "2 : None", ID = 2 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "3 : None", ID = 3 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "4 : None", ID = 4 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "5 : None", ID = 5 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "6 : None", ID = 6 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "7 : None", ID = 7 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "8 : None", ID = 8 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "9 : None", ID = 9 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "10 : None", ID = 10 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "11 : None", ID = 11 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "12 : None", ID = 12 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "13 : None", ID = 13 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "14 : None", ID = 14 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "15: None", ID = 15 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "16 : None", ID = 16 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "17 : None", ID = 17 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "18 : None", ID = 18 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "19 : None", ID = 19 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "20 : None", ID = 20 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "21 : None", ID = 21 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "22 : None", ID = 22 }); 
     SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "23 : None", ID = 23 }); 

另一個ViewModel類:XAML的 DataContext設置這個視圖模型類

/// <summary> 
    /// Event for Slot Command Button 
    /// </summary> 
    private ICommand mSlotCommand; 
    public ICommand SlotCommand 
    { 
     get 
     { 
      if (mSlotCommand == null) 
       mSlotCommand = new DelegateCommand(new Action(mSlotCommandExecuted), new Func<bool>(mSlotCommandCanExecute)); 

      return mSlotCommand; 
     } 
     set 
     { 
      mSlotCommand = value; 
     } 
    } 

    public bool mSlotCommandCanExecute() 
    { 
     return true; 
    } 

    public void mSlotCommandExecuted() 
    { 

    } 

但仍然是所有單選按鈕都得到檢查。如何清除它?

+0

請顯示SlotCommand的定義/創建 – GazTheDestroyer

+0

@GazTheDestroyer:更新:) – StonedJesus

0

明確將「Groupname」添加到您的單選按鈕。

<RadioButton GroupName="myGroup" Content="{Binding SlotButtons}" Margin="0,10,0,0" Command="{Binding SlotCommand}" Height="15" Width="80" HorizontalAlignment="Center" VerticalAlignment="Center" /> 

這將只允許一個單選按鈕檢查。