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

  • Home
  • SEARCH
  • 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 9209869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:56:55+00:00 2026-06-18T00:56:55+00:00

for my project, i am trying to create a signal channel generator which connects

  • 0

for my project, i am trying to create a signal channel generator which connects to a toolset and pushes signals into it.

the issue i have is that i have been given the project in a form where the code for the textboxes are in the codebehind file, and i would like them to be in the xaml.

i have a variable which controls the number of channels (viewmodels) which can be changed. which is able to create multiple instances of the same viewmodel on the window. this allows the ability to select different targets inside the tool whcih it is communicating with and be able to pump signals to each target.

here is the code currently in the XAML:

    <Window x:Class="SigGeneratorMVVM.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:SigGeneratorMVVM"
        Title="Signal Generator" Height="370" Width="734" >
    <StackPanel Name="MyWindow">
        <!--<TextBox Height="23" HorizontalAlignment="Left" Margin="91,20,0,0" Name="CurrentValDisplay" VerticalAlignment="Top" Width="120" />-->
    </StackPanel>
</Window>

here is the code for the mainwindow.cs

public partial class MainWindow : Window
    {
        List<ViewModel> gViewModels;

        int gNumChannels = 1;
        private System.Threading.Timer mViewUpdateTimer;
        private TimerCallback mViewTimerCallback;

        private UtilityParticipant mParticipant;

    public MainWindow()
    {
        InitializeComponent();

        // Connect as UtilityParticipant            
        ConnectMesh();

        gViewModels = new List<ViewModel>();           

        for (int i = 0; i < gNumChannels; i++)
        {
            gViewModels.Add(new ViewModel(mParticipant));

            TextBlock CurrentValueText = new TextBlock();
            CurrentValueText.Text = "Current Value:";
            CurrentValueText.Margin = new Thickness(5);

            TextBox CurrentValueBox = new TextBox();
            CurrentValueBox.Width = 120;
            CurrentValueBox.Name = "CurrentValDisplay" + i.ToString();
            CurrentValueBox.HorizontalAlignment =                                                System.Windows.HorizontalAlignment.Left;
            CurrentValueBox.Margin = new Thickness(10);
            CurrentValueBox.SetBinding(TextBox.TextProperty, "CurrentValue");

            //CurrentValDisplay.Name = "CurrentValDisplay" + i.ToString();
            //CurrentValDisplay.SetBinding(TextBox.TextProperty, "CurrentValue");

            TextBlock CurrentFrequencyText = new TextBlock();
            CurrentFrequencyText.Text = "Frequency:";
            CurrentFrequencyText.Margin = new Thickness(5);

            TextBox CurrentFrequencyBox = new TextBox();
            CurrentFrequencyBox.Width = 120;
            CurrentFrequencyBox.Name = "CurrentFrequencyDisplay" + i.ToString();
            CurrentFrequencyBox.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            CurrentFrequencyBox.Margin = new Thickness(10);
            CurrentFrequencyBox.SetBinding(TextBox.TextProperty, "Frequency");

            Slider FrequencySlider = new Slider();
            FrequencySlider.Width = 200;
            FrequencySlider.Name = "FrequencySet" + i.ToString();
            FrequencySlider.Value= 10;
            FrequencySlider.Maximum = 10;
            FrequencySlider.Minimum = 0.1;
            FrequencySlider.SetBinding(Slider.ValueProperty, "Frequency");                    

            //Create a new stackpanel
            StackPanel sp = new StackPanel();
            sp.Orientation = Orientation.Vertical;

            //Set DataContext of the StackPanel
            sp.DataContext = gViewModels[i];

            //Add controls created above to the StackPanel
            sp.Children.Add(CurrentValueText);
            sp.Children.Add(CurrentValueBox);
            sp.Children.Add(CurrentFrequencyText);
            sp.Children.Add(CurrentFrequencyBox);
            sp.Children.Add(FrequencySlider);

            //Add the StackPanel to the window

            MyWindow.Children.Add(sp);

        }

        mViewTimerCallback = this.UpdateView;
        mViewUpdateTimer = new System.Threading.Timer(mViewTimerCallback, null,     100, 20);
    }    

Update: I already have a ViewModel which has get set methods for each property (CurrentValue and Frequency for now), would it be sufficient to bind the DataTemplate and ItemsControl to that instead of creating a new model class?

    private SigGenChannel mSigGenChannel;

    //Constructor
    public ViewModel(UtilityParticipant aParticipant)
    {
        mSigGenChannel = new SigGenChannel(aParticipant);
    }


    public string CurrentValue
    {
        get
        {
            return mSigGenChannel.CurrentValue.ToString();
        }

        set
        {
            mSigGenChannel.CurrentValue = double.Parse(value);
            RaisePropertyChanged("CurrentValue");
        }
    }

    public double Frequency
    {
        get
        {
            return mSigGenChannel.Frequency;
        }

        set
        {
            mSigGenChannel.Frequency = value;
            RaisePropertyChanged("Frequency");
        }
    }

    public double Amplitude
    {
        get
        {
            return mSigGenChannel.Amplitude;
        }

        set
        {
            mSigGenChannel.Amplitude = value;
            RaisePropertyChanged("Amplitude");
        }
    }

    public void RefreshValue()
    {
        //A bit of a cheat, but we provide a means to poke the Viewmodel
        //And raise a property change event
        RaisePropertyChanged("CurrentValue");
    }

also this is the SigChannel model:

 class SigGenChannel
{
    #region Private members
    private UtilityParticipant mParticipant;
    private double mCurrentValue;
    private double mFrequency;
    private double mAmplitude;
    private double mTarget;
    private double mOffset;
    private double mCurrentStepTime;
    private DateTime mStartTime;
    private System.Threading.Timer mTimer;
    private TimerCallback mTCallback;
    private int mUpdateInterval = 10;
    #endregion

    #region Public members
    public double CurrentValue
    {
        get
        {
            return mCurrentValue;
        }
        set
        {
            mCurrentValue = value;
        }
    }        

    public double Frequency
    {
        get
        {
            return mFrequency;
        }

        set
        {
            mFrequency = value;
        }
    }

    public double Amplitude
    {
        get
        {
            return mAmplitude;
        }
        set
        {
            mAmplitude = value;
        }
    }

    public double Target
    {
        get
        {
            return mTarget;
        }
        set
        {
            mTarget = value;
        }
    }
    #endregion


    //Constructor
    public SigGenChannel(UtilityParticipant aParticipant)
    {
        mParticipant = aParticipant;
        mCurrentValue = 10;
        mFrequency = 200;
        mAmplitude = 100;
        mOffset = 0;
        mCurrentStepTime = 0;
        mStartTime = DateTime.Now;
        mTCallback = this.Update;
        mTimer = new System.Threading.Timer(mTCallback, null, 500, mUpdateInterval);
        //Array enumData = Enum.GetNames;
        //RefreshItems();
        //Temp Code....!
        Collection lCollection = mParticipant.GetCollection("DefaultNodeName.NodeStats");
        lCollection.Publish();
    }

    private void Update(object StateInfo)
    {
        TimeSpan span = DateTime.Now - mStartTime;
        mCurrentStepTime = span.TotalMilliseconds / (double)1000;

        mCurrentValue = (Math.Sin(mCurrentStepTime * (mFrequency * 2 * Math.PI)) * mAmplitude / 2) + mOffset;

        //Temp Code...!
        Collection lCollection = mParticipant.GetCollection("DefaultNodeName.NodeStats");
        Parameter lParameter = lCollection.GetParameter("CPUPercent");
        lParameter.SetValue(mCurrentValue);
        lCollection.Send();
  • 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-18T00:56:56+00:00Added an answer on June 18, 2026 at 12:56 am

    The current code is written in a way that does not follow the WPF suggested practices, and swimming against the current makes things a lot harder than then should be.

    What the code should be doing is:

    Create a (view)model class for a channel

    For example:

    class ChannelModel
    {
        public int Value { get; set; }
        public int Frequency { get; set; }
    }
    

    Use an ItemsControl instead of a StackPanel

    The WPF way of doing things like this is to bind controls to collections, so replace the StackPanel with an ItemsControl.

    Bind the ItemsControl to an ObservableCollection of the models

    Your main viewmodel should expose an ObservableCollection<ChannelModel> property and the control should bind to that directly:

    <ItemsControl ItemsSource="{Binding CollectionOfChannelModels}"/>
    

    This ensures that the control is automatically updated with any changes made to your collection without your needing to do anything else.

    Use a DataTemplate to specify how each model should render

    So far we ‘ve gotten the control to stay in sync with your channel collection, but we also need to tell it how each item (channel model) should be displayed. To do this, add a DataTemplate to the Resources collection of the ItemsControl:

    <ItemsControl.Resources>
      <DataTemplate DataType={x:Type local:ChannelModel}>
        <StackPanel Orientation="Horizontal">
          <TextBlock Text="Value" />
          <TextBox Text="{Binding Value}" />
        </StackPanel>
      </DataTemplate>
    </ItemsControl.Resources>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am trying to create a java project which can able to send push notification
I'm trying to create a project-like document, in that it contains subdocuments in a
I am trying to create a Project Template Wizard. For that I need a
I am trying to create nested routes for a project that has many boards.
In a MVC project am trying to create a view which has the Title
am trying to create a project that create a file, formatting it, create a
Im trying to create a RSS feed reader for a project that I working
I am trying to create an setup project in visual studio 2010 that clears
I have started a project trying to create a Ken Ken puzzle. If you
Xcode 4.2 iPhone Project I am trying to create a tableView that's populated from

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.