我是C ++开发人员,最近开始从事WPF工作。我很抱歉这个奇怪的标题,因为我不知道该放什么。在我的应用程序中,我需要动态生成2个包含按钮,标签,文本框,组合框等的组框。一旦完成,我需要对这些控件执行一些操作。
我有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个组合框:)在我的组合框中,我有A,B,C,D
作为项目。看看我如何从组合框中检索选定值的组合框绑定。在这里,我想对所有这些控件执行相同的操作,但是如果不同的值。我的意思是说我在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
两次创建组合框并根据选择的组合框选择值i
。即如果第一个组合框选择被更改,则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组合?对我来说这是一个棘手的情况。请帮助:)
答案 0 :(得分:1)
使用ID属性:
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);
}
但是你也可以使用你在instanciation设置的BitMask-Property:
public string BitMask {get;set;}
public void SelectedPreScalar(int Select)
{
m_controlRegs[0] &= Convert.ToByte(BitMask);
m_refClock[0] = Convert.ToByte(18432000 * 2);
}
根据您的决定,您的PCGenViewModel构造函数可能如下所示:
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 });
}
我建议使用第二个建议,因为如果添加第三个组合框,则无需更改SelectedPreScalar方法。