0

WPFアプリケーションを開発しています。私は基本的にC++開発者であり、最近C#に移行しました。私のアプリケーションでは、特定の操作を実行するボタンのセットを動的に生成しました。

各ボタンには周波数があり、キャプチャされて何らかの操作を実行するために使用されます。私のC++アプリケーションでは、次のように作成しました。

static const char *freqs[MAX_CLOCK_RANGE] = 
{
"12.0.",12.288","15.36","19.2","23.0","24.0","26.0" //MAX_CLOCK_RANGE is 7
};

for(int i = 0; i < MAX_CLOCK_RANGE; i++)
{
    buttonText = String(T("Set ")) + String(freqs[i])+ String(T(" MHz"));
    m_buttonSetFreq[i] = new TextButton(buttonText, String::empty);
    m_buttonSetFreq[i]->addButtonListener(this);
    addAndMakeVisible(m_buttonSetFreq[i]);      
}

int cnt = 0;
while(cnt < MAX_CLOCK_RANGE)
{
   if(button == m_buttonSetFreq[cnt])
   break;
   cnt++;       
}
if(cnt < MAX_CLOCK_RANGE)
{
   unsigned int val = String(freqs[cnt]).getDoubleValue() * 1000.0;
}
sendBuf[numBytes++] = 0x00; //SendBuf is unsigned char
sendBuf[numBytes++] = 0x00;
sendBuf[numBytes++] = (val >> 8) & 0xFF;
sendBuf[numBytes++] = val & 0xFF;

したがって、char配列からfreqの値を取得し、上記のように操作を実行します。Cntには、特定のボタンがクリックされた値があり、ボタンがクリックされた頻度を取得します。WPFアプリで次のように実行しました。

XAML:

<ListBox x:Name="SetButtonList" ItemsSource="{Binding}" >
                    <ListBox.ItemTemplate>
                        <DataTemplate >
                            <Grid>
                                <Button Name="FreqButton" Content="{Binding Path=SetButtonFreq}" Command="{Binding ElementName=SetButtonList, Path=DataContext.SetFreqCommand}" />                                    
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
</ListBox>

Xaml.cs:

ClockViewModel mClock = new ClockViewModel();

mClock.Add(new ClockModel("Set 12.0 MHz"));
mClock.Add(new ClockModel("Set 12.288 MHz"));
mClock.Add(new ClockModel("Set 15.36 MHz"));
mClock.Add(new ClockModel("Set 19.2 MHz"));
mClock.Add(new ClockModel("Set 23.0 MHz"));
mClock.Add(new ClockModel("Set 24.0 MHz"));
mClock.Add(new ClockModel("Set 26.0 MHz"));
SetButtonList.DataContext = mClock;

ViewModel:

ClockModel mCModel = new ClockModel();

private ICommand mSetFreqCommand;
    public ICommand SetFreqCommand
    {
        get
        {
            if (mSetFreqCommand == null)
                mSetFreqCommand = new DelegateCommand(new Action(SetFreqCommandExecuted), new Func<bool>(SetFreqCommandCanExecute));

            return mSetFreqCommand;
        }
        set
        {
            mSetFreqCommand = value;
        }
    }

    public bool SetFreqCommandCanExecute()
    {
        return true;
    }

    public void SetFreqCommandExecuted()
    {
        //How can I retrieve the Frequency of button clicked and perform same operation as done in C++
    }

モデル:

public String SetButtonFreq {get; set;}

クリックされた各ボタンの頻度を取得して、C ++コードで実行されるのと同じ手順を実行することは可能ですか?助けてください :)

4

1 に答える 1

2

2つの可能な解決策。

1番目:各ボタンのViewModelインスタンスのコマンドにバインドします。

<Window x:Class="WpfTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox ItemsSource="{Binding Clocks}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                <Button Content="{Binding Frequency}" 
                        Command="{Binding SetFrequency}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

次のビューモデルとコードビハインドを使用:

class ClockViewModel  // DataContext of ListBoxItem
{
    public double Frequency { get; set; }
    public ICommand SetFrequency
    {
        get
        {
            return new DelegateCommand((obj) => { 
              double freq = this.Frequency; 
              // ...
            });
        }
    }
}

class WindowViewModel 
{
    ObservableCollection<ClockViewModel> clocks_ = 
        new ObservableCollection<ClockViewModel>();
    public ObservableCollection<ClockViewModel> Clocks {get {return clocks_;}}
}

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var viewModel = new WindowViewModel();
        viewModel.Clocks.Add(new ClockViewModel() { Frequency = 10.123 });
        viewModel.Clocks.Add(new ClockViewModel() { Frequency = 42.0 });
        viewModel.Clocks.Add(new ClockViewModel() { Frequency = 3.14 });

        DataContext = viewModel;
    }
}

2番目):コマンドを使用できるWindowsビューモデルの一部にする必要がある場合CommandParameter(「3.13Mhzの​​設定」形式で表示するように更新):

<Window x:Class="WpfTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
  <Grid>
    <ListBox Name="list" ItemsSource="{Binding Clocks}">
      <ListBox.ItemTemplate>
        <DataTemplate>
          <Button Command="{Binding Path=DataContext.SetFrequency, 
                          ElementName=list}"
                  CommandParameter="{Binding}">
            <Button.Content>
              <TextBlock>
                <Run>Set </Run>
                <Run Text="{Binding Frequency}"/>
                <Run> Mhz</Run>
              </TextBlock>
            </Button.Content>
          </Button>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
  </Grid>
</Window>

これにより、ボタンのデータコンテキストがコマンドに渡されます。

class ClockViewModel
{
    public double Frequency { get; set; }
}

class WindowViewModel
{
    ObservableCollection<ClockViewModel> clocks_ = new ObservableCollection<ClockViewModel>();
    public ObservableCollection<ClockViewModel> Clocks
    {
        get { return clocks_; }
    }

    public ICommand SetFrequency
    {
        get{
            return new DelegateCommand((obj) => 
            { 
                var clock = obj as ClockViewModel;
                DoSendBufStuffWithFrequency(clock.Frequency);
            });}
    }

    private void DoSendBufStuffWithFrequency(double frequency)
    {
    }
}
于 2012-10-13T10:29:02.260 に答える