我是一名 C++ 开发人员,最近搬到了 wpf。我似乎遇到了一个棘手的情况,我必须根据单选按钮单击动态生成标签。在这里,我将首先向您展示我是如何生成 4 个单选按钮的。
XAML:
<Grid Grid.Row="0">
<ItemsControl ItemsSource="{Binding Children}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"
IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding RadioBase}" Margin="0,10,0,0" IsChecked="{Binding BaseCheck}" Height="15" Width="80" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
<Grid Grid.Row="1">
<Label Content="{Binding name}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
视图模型:
private bool sBaseCheck;
public bool BaseCheck
{
get { return this.sBaseCheck; }
set
{
this.sBaseCheck = value;
this.OnPropertyChanged("BaseCheck");
}
}
private int _ID;
public int ID
{
get
{
return _ID;
}
set
{
_ID = value;
OnPropertyChanged("ID");
}
}
private string _NAme;
public string name
{
get
{
return _NAme;
}
set
{
_NAme= value;
OnPropertyChanged("name");
}
}
private string _RadioBase;
public string RadioBase
{
get
{
return _RadioBase;
}
set
{
_RadioBase = value;
OnPropertyChanged("RadioBase");
}
}
另一个ViewModel 类:
public ObservableCollection<FPGAViewModel> Children { get; set; }
public FPGARadioWidgetViewModel()
{
Children = new ObservableCollection<FPGAViewModel>();
Children.Add(new FPGAViewModel() { RadioBase = "Base 0x0", ID = 0 });
Children.Add(new FPGAViewModel() { RadioBase = "Base 0x40", ID = 1 });
Children.Add(new FPGAViewModel() { RadioBase = "Base 0x80", ID = 2 });
Children.Add(new FPGAViewModel() { RadioBase = "Base 0xc0", ID = 3 });
}
这给了我 4 个带有上述内容的单选按钮。现在我想在每次单击单选按钮时生成 8 个标签。我在我的 C++ 应用程序中这样做了,如下所示:
for(i = 0; i < 0x40 / 8; i++)
{
reg = (i * 8);
m_registerLabel[i] = new Label(String::empty, String("Reg 0x") + String::toHexString(reg));
addAndMakeVisible(m_registerLabel[i]);
}
如果您注意到,它将创建 8 个带有值 as Reg 0x0, Reg 0x8, Reg 0x10, Reg 0x18
etc 的标签,因为 reg 已转换为十六进制字符串。Base 0x0
当我点击启动时,我想生成这样的东西。
我怎样才能在我的应用程序中实现这一点???