-1

我想将按钮放入 DataGrid 的单元格 ListBox 中。我在 DataGrid 周围搜索了一下,但只发现了两件事:

  1. 使用 DataGridTemplateColumn 并在模板中设置命令
  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 的单元格中创建几个命令绑定(或事件处理程序)。这样我只能重写 DataGridHyperlinkColumn.ElementStyle 的样式并只设置一个事件处理程序(因为我知道我不能在这里设置命令)。


更新

 <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

使用 将RelayCommand您的 Button 绑定到您的 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 代码:(
我使用 diffDataGrid.ItemsSource和 forListBox.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 回答
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 回答