-1

ボタンの DataGrid のセル ListBox に入れたいです。私は DataGrid をググりましたが、次の 2 つしか見つかりませんでした。

  1. テンプレートで DataGridTemplateColumn と set コマンドを使用する
  2. DataGridHyperLinkColumn を使用してイベント ハンドラを設定する

私は最初のバリアントを試しました(ボタンをクリックしても何も起こらず、ボタンのみとしてListBoxなしで使用した場合も同様です):

<DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <ListBox ItemsSource="{Binding diffs}">
                                        <ListBox.ItemTemplate>
                                            <DataTemplate>
                                                <Button Command="{Binding ElementName=Root, 
                                                        Path=DataContext.viewSimpleRoute, Mode=OneTime}"
                                                        CommandParameter="{Binding}">
                                                aaa
                                            </Button>

                                            </DataTemplate>
                                        </ListBox.ItemTemplate>
                                    </ListBox>

DataGrid のセルにいくつかのコマンド バインディング (またはイベント ハンドラー) を作成したいので、2 番目のバリアントは使用できません。このようにして、DataGridHyperlinkColumn.ElementStyle のスタイルのみを書き換えて、1 つのイベント ハンドラーのみを設定できます (ここでコマンドを設定できないことがわかっているため)。


アップデート

 <DataTemplate DataType="{x:Type tdm:TVZ+SimpleTvzDiffModel}">

                <StackPanel Orientation="Vertical">
                    <Button Width="100" Height="23" 
                                            Command="{Binding ElementName=Root, Path=DataContext.viewSimpleRoute, Mode=OneTime}"
                                            CommandParameter="{Binding}">
                        WORKS
                    </Button>
                    <DataGrid ItemsSource="{Binding diffRoutes}" AutoGenerateColumns="False">
                        <DataGrid.Columns>                            
                            <DataGridTemplateColumn Header="Маршруты">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <Button Width="100" Height="23" 
                                            Command="{Binding ElementName=Root, Path=DataContext.viewSimpleRoute, Mode=OneTime}"
                                            CommandParameter="{Binding}">
                                            DOES NOT WORK
                                        </Button>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>

なんで?

4

2 に答える 2

0

@BastiOnWpf と非常によく似た処理を行いましたが、次のように xaml 内でわずかに変更しました。

<UserControl.Resources>
    <DataTemplate x:Key="RowButtons">
        <StackPanel Orientation="Horizontal">
            <Button Content="SomeCommand" Command="{Binding SomeCommand}" Width="50"/>
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>

        <ListView Name="ListView" Height="70" Width="700"
                  ItemsSource="{Binding diffs}">
            <ListView.View>
                <GridView>
                    <GridViewColumn CellTemplate="{StaticResource RowButtons}"/>
                    <!-- Rows goes here-->
                </GridView>
            </ListView.View>
        </ListView>

ご覧のとおり、私のリソース内で、datatemplateあなたが追加できるようにしたいコマンドを に設定しましたlistbox

ビューモデル;

        private ICommand _Command;
        public ICommand SomeCommand
        {
            get
            {
                if (this._Command == null)
                {
                    this._Command = new new RelayCommand(this.SomeMethod); //The method/procedure you want to use for the listbox
                }
                return this._Command;
            }
        }

も使用しました。RelayCommandご覧bindのとおり、ビューモデルからビューへのアイテムです。

お役に立てれば :)。

于 2013-03-28T15:58:50.400 に答える
0

RelayCommandButton を ViewModel にバインドするには、 を使用します。
ソリューションを簡素化するためViewModelに、ウィンドウ自体にプロパティを出力して書き込みました。
それを変更することを検討する必要があります。

Class MainWindow

      Public Property diffs As List(Of String)
      Public Property ButtonCommand As New RelayCommand(AddressOf ButtonClick)

      Private Sub ButtonClick()
      End Sub

End Class


XAML コード: (単純化するために、 for と for の
差分を使用しました。DataGrid.ItemsSourceListBox.ItemsSource

<Window x:Class="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" 
    x:Name="Root">
  <Grid>
    <DataGrid x:Name="DataGridButtons" ItemsSource="{Binding ElementName=Root, Path=diffs}">
      <DataGrid.Columns>
        <DataGridTemplateColumn>
          <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
              <ListBox ItemsSource="{Binding ElementName=Root, Path=diffs}">
                <ListBox.ItemTemplate>
                  <DataTemplate>
                    <Button Width="100" Height="23" Command="{Binding ElementName=Root, Path=ButtonCommand, Mode=OneTime}" CommandParameter="{Binding}">aaa</Button>
                  </DataTemplate>
                </ListBox.ItemTemplate>
              </ListBox>
            </DataTemplate>
          </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
      </DataGrid.Columns>
    </DataGrid>
  </Grid>
</Window>



RelayCommand クラスのソース:

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows.Input
Imports System.Diagnostics

Public Class RelayCommand
    Implements ICommand

    ReadOnly _execute As Action(Of Object)
    ReadOnly _canExecute As Predicate(Of Object)

    Public Sub New(execute As Action(Of Object))
        Me.New(execute, Nothing)
    End Sub

    Public Sub New(execute As Action(Of Object), canExecute As Predicate(Of Object))
        If execute Is Nothing Then
            Throw New ArgumentNullException("execute")
        End If

        _execute = execute
        _canExecute = canExecute
    End Sub

    <DebuggerStepThrough()> _
    Public Function CanExecute(parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
        Return If(_canExecute Is Nothing, True, _canExecute(parameter))
    End Function

    Public Sub Execute(parameter As Object) Implements System.Windows.Input.ICommand.Execute
        _execute(parameter)
    End Sub

    Public Sub UpdateCanExecute()
        RaiseEvent CanExecuteChanged(Me, New EventArgs())
    End Sub

    Public Sub RaiseCanExecuteChanged()
        CommandManager.InvalidateRequerySuggested()
    End Sub

    ''' <summary>
    ''' To prevent the following calls to invalidate the CanExecuteProperty
    ''' StartCommand.RaiseCanExecuteChanged()
    ''' StopCommand.RaiseCanExecuteChanged()
    ''' </summary>
    ''' <remarks></remarks>
    Public Custom Event CanExecuteChanged As EventHandler Implements System.Windows.Input.ICommand.CanExecuteChanged
        AddHandler(ByVal value As EventHandler)
            AddHandler CommandManager.RequerySuggested, value
        End AddHandler
        RemoveHandler(ByVal value As EventHandler)
            RemoveHandler CommandManager.RequerySuggested, value
        End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
        End RaiseEvent
    End Event

    'Public Event CanExecuteChanged(sender As Object, e As System.EventArgs) Implements System.Windows.Input.ICommand.CanExecuteChanged

End Class
于 2013-03-28T13:59:26.977 に答える