0

我是一名 C++ 开发人员,最近开始研究 WPF。我很抱歉这个奇怪的标题,因为我不知道该放什么。在我的应用程序中,我需要动态生成 2 个包含按钮、标签、文本框、组合框等的组合框。一旦完成,我需要对这些控件执行一些操作。

我有 2 个 xaml 文件PCMGenView.xamlPCMGenWidgetView.xaml其中PCMGenWidgetView.xaml文件具有组框并被添加到PCMGenView.xaml文件中。我还有 2 个视图模型类和一个模型类。好吧,让我向您展示我是如何做到的:

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 组合?这对我来说是一个棘手的情况。请帮忙 :)

4

1 回答 1

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); 
    }

但您也可以使用在实例化时设置的 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 方法。

于 2012-10-19T07:49:02.680 回答