2012-03-29 19 views
0

今、私はWPF経由でWindowsサービスコンソールプログラムを開発したいと思っています。各サービスはlistviewの項目にあります。 HTTP:コンソールの画像の
リンク//i.stack.imgur.com/eMX1d.jpg したがって、私は以下のように定義されたクラス:リストビュー内の各ボタンのクリックイベントをバインドする方法はありますか?

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

をし、対応するリストビューXAMLは以下の通りである:

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


私はServiceItemインスタンスのコレクションを作成し、リストビューにバインド:

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

ここで問題は、開始/一時停止/停止イベントメソッドを各ボタンにバインドする必要があるかどうかです。

答えて

0

使用のICommandインタフェース

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

とあなたのServiceItemが持っている...ボタン

ビューの項目テンプレートのバインディング公共のICommandプロパティ

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

* WPF を検索DelegateCommandRelayCommand

0

ボタンクリックイベントを使用する代わりに、コマンドを使用する必要があります。

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx。この記事を読んでください(具体的には、「Relaying Command Logic」の部分)。 RelayCommandに似たクラスを実装します。その後、ServiceItemにStartCommand、PauseCommand、StopCommandという3つのコマンドがあります。これらをxamlのボタンのコマンドプロパティでバインドします。

+0

_italic_ありがとう!_italic_ –

関連する問題