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 7704533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:42:05+00:00 2026-05-31T23:42:05+00:00

Now I wanna develope a windows service console program via WPF. Each service is

  • 0

Now I wanna develope a windows service console program via WPF. Each service is in an item of listview.
link of the image of console :https://i.stack.imgur.com/eMX1d.jpg
Therefore, I defined a class as below:

/// <summary>
/// 
/// </summary>
class ServiceItem
{
    #region Fields
    /// <summary>
    /// Service Name
    /// </summary>
    private string _serviceName;
    /// <summary>
    /// Start Button
    /// </summary>
    private Button _startButton;
    /// <summary>
    /// Pause Button
    /// </summary>
    private Button _pauseButton;
    /// <summary>
    /// Stop Button
    /// </summary>
    private Button _stopButton;
    #endregion

    #region Properties
    /// <summary>
    /// Service Controller
    /// </summary>
    private ServiceController Service
    {
        get
        {
            var services = ServiceController.GetServices();
            var service = services.AsQueryable().Where(o => o.ServiceName == _serviceName).Select(o => o).FirstOrDefault();
            return service;
        }
    }
    /// <summary>
    /// Service Display Name
    /// </summary>
    public string ServiceDisplayName
    {
        get { return Service.DisplayName; }
    }
    /// <summary>
    /// Service Name
    /// </summary>
    private string ServiceName
    {
        get { return Service.ServiceName; }
    }
    /// <summary>
    /// Service Current Status
    /// </summary>
    public string Status
    {
        get
        {
            var status = Service.Status;
            switch (status)
            {
                case ServiceControllerStatus.StartPending:
                    return "StartPending";
                case ServiceControllerStatus.Running:
                    return "Running";//
                case ServiceControllerStatus.PausePending:
                    return "PausePending";//
                case ServiceControllerStatus.Paused:
                    return "Paused";//
                case ServiceControllerStatus.StopPending:
                    return "StopPending";//
                case ServiceControllerStatus.Stopped:
                    return "Stopped";//
                default:
                    return "Unknow";//
            }
        }
    }
    /// <summary>
    /// Service Start Mode
    /// </summary>
    public string StartMode
    {
        get
        {
            var startMode = ServiceHelper.GetServiceStartMode(_serviceName);
            switch (startMode)
            {
                case ServiceStartMode.Automatic:
                    return "Automatically";//
                case ServiceStartMode.Disabled:
                    return "Disabled";//
                case ServiceStartMode.Manual:
                    return "Manual";//
                default:
                    return "Unkown";//
            }
        }
    }

    /// <summary>
    /// Start Button
    /// </summary>
    public Button StartButton
    {
        get
        {
            return _startButton;
        }
    }
    /// <summary>
    /// Pause Button
    /// </summary>
    public Button PauseButton
    {
        get
        {
            return _pauseButton;
        }
    }
    /// <summary>
    /// Stop Button
    /// </summary>
    public Button StopButton
    {
        get
        {
            return _stopButton;
        }
    }
    #endregion

    #region Constructor
    /// <summary>
    /// Init Service Instance
    /// </summary>
    public ServiceItem(string serviceName)
    {
        _serviceName = serviceName;
        _startButton = new Button();
        _pauseButton = new Button();
        _stopButton = new Button();

        _startButton.Content = Resources.Media_Play;
        _pauseButton.Content = Resources.Media_Pause;
        _stopButton.Content = Resources.Media_Stop;

        _startButton.IsEnabled = Service.Status != ServiceControllerStatus.Running;
        _pauseButton.IsEnabled = Service.Status != ServiceControllerStatus.Paused;
        _stopButton.IsEnabled = Service.Status != ServiceControllerStatus.Stopped;

        _startButton.Click += StartButton_Click;
        _pauseButton.Click += PauseButton_Click;
        _stopButton.Click += StopButton_Click;
    }

    #endregion

    #region Methods
    /// <summary>
    /// Start Service
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void StartButton_Click(object sender, RoutedEventArgs e)
    {
        ServiceHelper.StartService(Service);
    }
    /// <summary>
    /// Stop Service
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void StopButton_Click(object sender, RoutedEventArgs e)
    {
        ServiceHelper.StopService(Service);
    }
    /// <summary>
    /// Pause Service
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void PauseButton_Click(object sender, RoutedEventArgs e)
    {
        ServiceHelper.PauseService(Service);
    }
    /// <summary>
    /// Continue Service
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void ResumeButton_Click(object sender, RoutedEventArgs e)
    {
        ServiceHelper.ResumeService(Service);
    }
    #endregion
}

and the corresponding listview xaml is as below:

 <ListView x:Name="ServiceList" Background="#353535" BorderBrush="#353535" Foreground="White" Height="295" ItemsSource="{Binding}">
            <ListView.View>
                <GridView AllowsColumnReorder="True">
                    <GridView.Columns>
                        <GridViewColumn Width="auto" Header="Name" DisplayMemberBinding="{Binding ServiceDisplayName}"></GridViewColumn>
                        <GridViewColumn Width="auto" Header="Status" DisplayMemberBinding="{Binding Status}"></GridViewColumn>
                        <GridViewColumn Width="auto" Header="StartMode" DisplayMemberBinding="{Binding StartMode}"></GridViewColumn>
                        <GridViewColumn Width="auto" Header="Operation">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <Grid x:Name="grdOperations" x:Uid="grdOperations">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                        </Grid.ColumnDefinitions>
                                        <Button Grid.Column="0" Height="24" Width="24" Style="{StaticResource ImageButton}" >
                                            <Image Source="Resources/Media Play.png"></Image>
                                        </Button>
                                        <Button Grid.Column="1" Height="24" Width="24" Margin="1,0,1,0" Style="{StaticResource ImageButton}">
                                            <Image Source="Resources/Media Pause.png"></Image>
                                        </Button>
                                        <Button Grid.Column="2" Height="24" Width="24" Style="{StaticResource ImageButton}">
                                            <Image Source="Resources/Media Stop.png"></Image>
                                        </Button>
                                    </Grid>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>

                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>

I created the ServiceItem instance collection and bind it to the ListView:

var services = new List<ServiceItem>()
        {
            new ServiceItem("AxInstSV"),
            new ServiceItem("PeerDistSvc")
        };
        ServiceList.ItemsSource = services;

now the problem is how counld/should I bind the start/pause/stop event method to each button?

  • 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-05-31T23:42:07+00:00Added an answer on May 31, 2026 at 11:42 pm

    use ICommand interface binding for button

    View’s item template..

    <Button Command="{Binding PlayCommand}" Height="24" Width="24" Style="{StaticResource ImageButton}" >
        <Image Source="Resources/Media Play.png"></Image>
    </Button>
    <Button Command="{Binding PauseCommand}" Height="24" Width="24" Style="{StaticResource ImageButton}" >
        <Image Source="Resources/Media Pause.png"></Image>
    </Button>
    

    and your ServiceItem have public ICommand Properties

    public ICommand PlayCommand{ get; private set;}
    public ICommand PauseCommand{ get; private set;}
    
    #region Constructor
    public ServiceItem(string serviceName)
    {
        _serviceName = serviceName;
    
        //DelegateCommand implement ICommand interface.
        PlayCommand = new DelegateCommand(Play); 
    
        //Alternate way you can use lamda expression
        PauseCommand = new DelegateCommand(()=> ServiceHelper.PauseService(Service));
    }
    #endregion
    
    private void Play()
    {
        ServiceHelper.StartService(Service);
    }
    

    *search for WPF DelegateCommand or RelayCommand

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a simple netbeans plugin (see this tutorial ) and now i wanna
Now I have a stack of free time on my hands, I wanna get
I have created a sample report with crystal report 10. Now, I wanna to
I have a project on intellij and now i wanna run it on intellinj
i have an array which is populated in the spinner adapter. now i wanna
I have create a socket with CFSocket. My program is correct but now i
I begin to program some handheld program as hobby, right now i currently have
I have a view for logout,now I wanna when user click on an anchor
I'm using node.js in my server side. Now I wanna run some binary file
I have a form which takes multiple inputs from user. Now I wanna show

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.