Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8691573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:08:43+00:00 2026-06-13T00:08:43+00:00

I am developing a WPF application. I am basically a C++ developer and have

  • 0

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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:08:44+00:00Added an answer on June 13, 2026 at 12:08 am

    Two possible solutions.

    1st: Bind to command in ViewModel instance of each button:

    <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>
    

    With following View Models and code behind:

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

    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):

    <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>
    

    This passes the buttons data context to the command:

    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)
        {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing wpf application in C#. I have one button on which I
I am developing a WPF application, and have a TextBlock which I want to
We are developing a WPF Application which should support multiple Languages. Since we have
Context: I'm developing a WPF application which will contain a lot of different screens.
I am developing a WPF application, and I need your advice. I have to
We are developing a WPF application which uses Telerik's suite of controls and everything
I'm developing a WPF application which reads and writes XML data. I'm coming from
I'm developing an application using the WAF (WPF Application Framework) which is based on
I am developing an application in Visual Studio using C# and WPF. I have
I am developing wpf application. I have one static world map of 500 width

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.