I am a C++ developer and recently started working on WPF. I am sorry for the weird title since I wasnt sure what to put. In my app, I need to dynamically generate 2 groupboxes which contain buttons, labels, textbox, combobox’s and so on. And once its done, I need to perform some operation on these controls.
I have 2 xaml files PCMGenView.xaml and PCMGenWidgetView.xaml where PCMGenWidgetView.xaml file has the groupbox and is added to the PCMGenView.xaml file. I also have 2 viewmodel classes and a model class. Well let me show you how I have done it:
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);
}
Now this gives me 2 groupboxes on startup 🙂 In my combobox I have A,B,C,D as items. Have a look at the combobox binding at how I am able to retrieve the selected value from the combobox. Here I wanna perform the same operation on all these controls but if different values. Well I mean to say something like this which I did in my C++ app:
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;
}
If you notice above code, you will find the for loop creating the combobox two times and based on which ever combobox is selected value is taken in i. I.e. if first combobox selection is changed then unsigned char bitMask = (0 == i) ? 0xCF : 0x3F; becomes unsigned char bitMask = (0 == 0) ? 0xCF : 0x3F; if second then, unsigned char bitMask = (0 == 1) ? 0xCF : 0x3F;
This is my query. How can I get to know which combobox I have used. Whether I have used PCM Gen 1 combo or PCM Gen 2 combo? This is a tricky situation for me. please help 🙂
Use an ID-property:
But you also can use a BitMask-Property, which you set at the instanciation:
Depending on how you decided, your PCGenViewModel-constructor could look like:
or
I would recommend using the second suggestion, because if you add a third groupbox, you dont have to change your SelectedPreScalar-method.