我是C ++开发人员,最近转向C#。我在我的wpf应用程序中使用MVVM模式。我正致力于动态生成radiobuttons。那么要求很简单,我需要生成24个Radiobutton,这样一次只能检查一个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]);
}
现在这就是我想要实现的目标:
Content = 0: None
到23: None
。请帮助:)
答案 0 :(得分:2)
您不需要SlotCommand
。而是将每个单选按钮的IsChecked
属性绑定到viewmodel上的布尔属性。
例如: 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
设置器中执行此操作。
答案 1 :(得分: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设置为此ViewModel类
/// <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()
{
}
但仍然检查所有的无线电按钮。如何清除它?
答案 2 :(得分:0)
将“Groupname”明确添加到单选按钮。
<RadioButton GroupName="myGroup" Content="{Binding SlotButtons}" Margin="0,10,0,0" Command="{Binding SlotCommand}" Height="15" Width="80" HorizontalAlignment="Center" VerticalAlignment="Center" />
这将只允许一个单选按钮进行检查。