如何在两个动态生成的组合蛋白上使用不同的值
How to use different values on two Dynamically generated Comboboxes
我是C 开发人员,最近开始从事WPF。我不确定要放什么的标题感到抱歉。在我的应用中,我需要动态生成2个组盒,其中包含按钮,标签,文本框,Combobox等。一旦完成,我需要对这些控件执行一些操作。
我有2个XAML文件PCMGenView.xaml
和PCMGenWidgetView.xaml
,其中PCMGenWidgetView.xaml
文件具有GroupBox,并添加到PCMGenView.xaml
文件中。我也有2个ViewModel类和一个模型类。好吧,让我向您展示我是如何完成的:
pcmgenview.xaml
<UserControl.Resources>
<DataTemplate x:Key="PGenDataTemplate">
<WrapPanel>
<TextBlock Text="{Binding Description}" Margin="5,5,0,0"/>
<local:PCMGenWidgetView Margin="5,10,5,5"/>
</WrapPanel>
</DataTemplate>
</UserControl.Resources>
<Grid>
<ItemsControl ItemTemplate="{StaticResource PGenDataTemplate}" ItemsSource="{Binding PGenWidgets}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
PCMGENWIDGETVIEW.XAML:
<Grid>
<GroupBox Height="Auto" HorizontalAlignment="Stretch" Margin="10" Name="groupBox1" VerticalAlignment="Stretch" Width="Auto">
<Grid >
<ComboBox Grid.Column="1" ItemsSource="{Binding PreScalarList}" SelectedItem="{Binding SelectedPreScalarList, Mode=OneWayToSource}" SelectedIndex="0" Height="23" HorizontalAlignment="Center" Margin="0,0,0,0" Name="PCMGenControlCombo" VerticalAlignment="Center" Width="110" />
// Radio Button, Buttons etc are present too
</Grid>
</GroupBox>
</Grid>
pcmgenwidgetView.xaml.cs:
public partial class PCMGenWidgetView : UserControl
{
PCMGenWidgetViewModel mPCMGenWidgetViewModel = new PCMGenWidgetViewModel();
public PCMGenWidgetView()
{
InitializeComponent();
this.DataContext = mPCMGenWidgetViewModel;
}
}
pcmgenviewmodel:
public ObservableCollection<PCMGenWidgetViewModel> PGenWidgets { get; set; }
public PCMGenViewModel()
{
PGenWidgets = new ObservableCollection<PCMGenWidgetViewModel>();
PGenWidgets.Add(new PCMGenWidgetViewModel { Description = "PCM Generator 1", ID = 0 });
PGenWidgets.Add(new PCMGenWidgetViewModel { Description = "PCM Generator 2", ID = 1 });
}
PCMGENWIDGETVIEWMODEL:
private string _description;
public string Description
{
get
{
return _description;
}
set
{
_description = value;
OnPropertyChanged("Description");
}
}
public ObservableCollection<string> PreScalarList
{
get { return _PreScalarList; }
set
{
_PreScalarList = value;
OnPropertyChanged("PreScalarList");
}
}
private string _selectedPreScalarList;
public string SelectedPreScalarList
{
get { return _selectedPreScalarList; }
set
{
_selectedPreScalarList = value;
int Listvalue = PreScalarList.IndexOf(_selectedPreScalarList);
int ListFinalVal = Listvalue + 1;
SelectedPreScalar(ListFinalVal);
OnPropertyChanged("SelectedPreScalarList");
}
}
private int _ID;
public int ID
{
get
{
return _ID;
}
set
{
_ID = value;
OnPropertyChanged("ID");
}
}
public void SelectedPreScalar(int Select)
{
int bitMask;
bitMask = (0 == ID) ? 0xCF : 0x3F; // ID always shows 0
m_controlRegs[0] &= Convert.ToByte(bitMask);
//m_refClock[0] = Convert.ToByte(18432000 * 2);
}
现在,这为我提供了2个启动的组箱:)在我的ComboBox中,我将A,B,C,D
作为项目。看看我能够从ComboBox检索所选值的结合。在这里,我想对所有这些控件执行相同的操作,但如果值不同。好吧,我的意思是说我在C 应用中所做的类似的话:
for( i = 0; i < 2; i++) //Constructor: Here 2 is used because we have 2 groupboxes
{
m_pcmGenPrescalar[i] = new ComboBox(String::empty);
m_pcmGenPrescalar[i]->addItem(String(T("div 1")), 1);
m_pcmGenPrescalar[i]->addItem(String(T("div 15")), 2);
m_pcmGenPrescalar[i]->addItem(String(T("div 255")), 3);
m_pcmGenPrescalar[i]->addItem(String(T("div 65535")), 4);
m_pcmGenPrescalar[i]->setEditableText(false);
m_pcmGenPrescalar[i]->setSelectedId(1, true);
m_pcmGenPrescalar[i]->addListener(this);
addAndMakeVisible(m_pcmGenPrescalar[i]);
}
for( i = 0; i < 2; i++) //Here 2 is used because we have 2 groupboxes
{
if(m_pcmGenPrescalar[i] == comboBox) //PreScalar Combobox
{
unsigned char bitMask = (0 == i) ? 0xCF : 0x3F; Takes the value of i
m_controlRegs[0] &= bitMask;
m_refClock[i] = 18432000 * 2;
}
如果您注意到上述代码,则会发现for loop
两次创建ComboBox,并基于i
中选择的combobox值。IE。如果更改了第一个Combobox选择,则unsigned char bitMask = (0 == i) ? 0xCF : 0x3F;
将变为unsigned char bitMask = (0 == 0) ? 0xCF : 0x3F;
,如果第二,则unsigned char bitMask = (0 == 1) ? 0xCF : 0x3F;
这是我的查询。我怎么知道我使用了哪种组合。我是使用PCM Gen 1组合还是PCM Gen 2组合?对我来说,这是一个棘手的情况。请帮助:)
使用ID-Property:
public int ID {get;set;}
public void SelectedPreScalar(int Select)
{
int bitMask;
bitMask = (0 == ID) ? 0xCF : 0x3F;
m_controlRegs[0] &= Convert.ToByte(bitMask);
m_refClock[0] = Convert.ToByte(18432000 * 2);
}
但是您还可以使用bitmask-property,您可以在Instanciation上设置:
public string BitMask {get;set;}
public void SelectedPreScalar(int Select)
{
m_controlRegs[0] &= Convert.ToByte(BitMask);
m_refClock[0] = Convert.ToByte(18432000 * 2);
}
根据您的决定的方式,您的pcgenviewmodel-constructor看起来像:
public PCMGenViewModel()
{
PGenWidgets = new ObservableCollection<PCMGenWidgetViewModel>();
PGenWidgets.Add(new PCMGenWidgetViewModel { Description = "PCM Generator 1", BitMask="0xCF" });
PGenWidgets.Add(new PCMGenWidgetViewModel { Description = "PCM Generator 2", BitMask="0x3F" });
}
或
public PCMGenViewModel()
{
PGenWidgets = new ObservableCollection<PCMGenWidgetViewModel>();
PGenWidgets.Add(new PCMGenWidgetViewModel { Description = "PCM Generator 1", ID=0 });
PGenWidgets.Add(new PCMGenWidgetViewModel { Description = "PCM Generator 2", ID=1 });
}
我建议使用第二个建议,因为如果添加第三个组框,则不必更改所选的Prescalar-hethod。
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- C++中的动态铸造故障
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 可组合的lambda/std::函数与std::可选
- 内联映射初始化的动态atexit析构函数崩溃
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 动态填充的组合框不显示文本,列表未定义
- 如何将组合框放入我的C++动态库中
- 如何在两个动态生成的组合蛋白上使用不同的值
- C++动态创建的MFC视觉工作室填充组合框
- 哪种技术用于动态更新组合框中的值
- 动态调整组合框的宽度,以便可以显示整个字符串
- 我可以在C++中动态组合函数和类型吗?
- QML 动态组合框条目
- 鼠标按下QComboBox的插槽-如果用户单击组合框,则动态填充项目
- 使用多个动态数组合并排序
- 动态创建的组合框中的CBN_SELCHANGE