我是 C++ 开发人员,从上周开始我开始研究 C# 和 WPF。我有一个关于在我的代码中使用结构的非常简单的查询。我的 xaml 类中有一个标签和按钮,应该是动态生成的。我在 C++ 中开发如下:
typedef struct
{
String ChannelName;
bool available;
} Voltage_Channel;
Voltage_Channel *m_voltageChannels;
Voltage_Channel redhookChannels[6] = {
{"", false},
{"VDD_IO_AUD", true},
{"VDD_CODEC_AUD", true},
{"VDD_DAL_AUD", true},
{"VDD_DPD_AUD", true},
{"VDD_PLL_AUD", true},
};
m_voltageChannels = redhookChannels;
int cnt = 0;
int MAX_ADC =6;
while(cnt < MAX_ADC)
{
m_labelChannel[cnt] = new Label(m_voltageChannels[cnt].ChannelName); //Generates labels based on count
m_setButton[cnt] = new TextButton("Set"); //generates Buttons based on Count
m_setButton[cnt]->addButtonListener(this); // Event for the Button
if(m_voltageChannels[cnt].available)
{
addAndMakeVisible(m_labelChannel[cnt]); //Displays if channel is available
addAndMakeVisible(m_setButton[cnt]);
}
}
这会生成标签和按钮 6 次,如果频道可用,则显示它。
我的Xaml文件中有以下内容:
<Label Grid.Column="0" Content="{Binding VoltageLabel}" Name="VoltageLabel" />
<Button Grid.Column="1" Content="Set" Command="{Binding Path=SetButtonCommand}" Name="VoltageSetbtn" />
标签和按钮属性:
string voltageLabel;
public string VoltageLabel
{
get { return voltageLabel; }
set
{
voltageLabel = value;
OnPropertyChanged("VoltageLabel");
}
}
// Event for Set Button Click
private ICommand mSetCommand;
public ICommand SetButtonCommand
{
get
{
if (mSetCommand == null)
mSetCommand = new DelegateCommand(new Action(SetCommandExecuted), new Func<bool>(SetCommandCanExecute));
return mSetCommand;
}
set
{
mSetCommand = value;
}
}
public bool SetCommandCanExecute()
{
return true;
}
public void SetCommandExecuted()
{
// Body of the method
}
是否可以使用结构动态生成这些控件,看看我在 C+ 应用程序中是如何做到的?
请帮忙