I am developing a WPF application. I am basically a C++ developer and have recently shifted to C#. In my application I have dynamically generated set of buttons which perform a specific operation.
Each button has a frequency to it which is captured and is used to perform some operation. In my C++ Application, I had created as follows:
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;
Thus it takes the value of freq from the char array and performs operation on it as given above. Cnt has the value of which particular button is clicked and takes the frequency of button clicked. I did it in my WPF app as follows:
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++
}
Model:
public String SetButtonFreq {get; set;}
Is it possible to get the freq of each button clicked and perform the same steps as done in C++ code??? Please help 🙂
Two possible solutions.
1st: Bind to command in ViewModel instance of each button:
With following View Models and code behind:
2nd): If the Command should be part of the windows view model you could use
CommandParameter(Updated to show the in “Set 3.13 Mhz” format):This passes the buttons data context to the command: