gpt4 book ai didi

c# - 如何在两个动态生成的组合框上使用不同的值

转载 作者:行者123 更新时间:2023-11-28 07:54:40 25 4
gpt4 key购买 nike

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

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

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作为项目。查看组合框绑定(bind),了解我如何能够从组合框中检索选定的值。在这里,我想对所有这些控件执行相同的操作,但如果值不同。好吧,我想说的是我在 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 . IE。如果更改了第一个组合框选择,则 unsigned char bitMask = (0 == i) ? 0xCF : 0x3F;变成 unsigned char bitMask = (0 == 0) ? 0xCF : 0x3F;如果是第二个,unsigned char bitMask = (0 == 1) ? 0xCF : 0x3F;

这是我的查询。我怎样才能知道我使用了哪个组合框。我使用的是 PCM Gen 1 combo 还是 PCM Gen 2 combo?这对我来说是一个棘手的情况。请帮助:)

最佳答案

使用 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 方法。

关于c# - 如何在两个动态生成的组合框上使用不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12968622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com