如何在两个动态生成的组合蛋白上使用不同的值

How to use different values on two Dynamically generated Comboboxes

本文关键字:组合 动态 两个      更新时间:2023-10-16

我是C 开发人员,最近开始从事WPF。我不确定要放什么的标题感到抱歉。在我的应用中,我需要动态生成2个组盒,其中包含按钮,标签,文本框,Combobox等。一旦完成,我需要对这些控件执行一些操作。

我有2个XAML文件PCMGenView.xamlPCMGenWidgetView.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。