0

私は C++ 開発者で、最近 WPF に取り組み始めました。何を入れたらいいのか分からなかったので変なタイトルですみません。私のアプリでは、ボタン、ラベル、テキスト ボックス、コンボ ボックスなどを含む 2 つのグループ ボックスを動的に生成する必要があります。完了したら、これらのコントロールに対していくつかの操作を実行する必要があります。

2 つの xaml ファイルがPCMGenView.xamlありPCMGenWidgetView.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、コンボボックスが 2 回作成され、どちらのコンボボックスが選択されたかに基づいて値が取得されることがわかりますi。つまり、最初のコンボボックスの選択が変更された場合、2 番目の場合は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 });            
    }

3番目のグループボックスを追加する場合、SelectedPreScalarメソッドを変更する必要がないため、2番目の提案を使用することをお勧めします。

于 2012-10-19T07:49:02.680 に答える