代码之家  ›  专栏  ›  技术社区  ›  Crekate

DataGrid绑定内的列表框

  •  1
  • Crekate  · 技术社区  · 7 年前

    在xaml中绑定有问题。我有一个数据网格,里面有一个按钮列表框。我需要将按钮的命令参数绑定到datagrid的items id。我尝试过使用relativesource,使用id命名隐藏列,并尝试绑定列datacontext,但没有任何效果。我还尝试绑定到datagrid中的selecteditem,命令可以正常工作,但是canexecute不能,因为每个按钮都是根据所选行而不是实际所在行进行更新的。 这是我的代码:

    意见

    <DataGrid
        ItemsSource="{Binding Orders}"
        AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn  Header="Order date" Binding="{Binding OrderDate}" />
            <materialDesign:MaterialDataGridTextColumn Header="Status" Binding="{Binding Status}"/>
            <materialDesign:MaterialDataGridTextColumn Header="Last update" Binding="{Binding LastUpdate}"/>
            <materialDesign:MaterialDataGridTextColumn Header="Managed by" Binding="{Binding SomePerson}"/>
            <materialDesign:MaterialDataGridTextColumn Header="Number" Binding="{Binding SomeNumber}"/>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="{Binding ShowDetailsCommandViewModel.Name}" Command="{Binding ShowDetailsCommandViewModel.Command}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Actions">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ListBox ItemsSource="{Binding Actions}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <Button Content="{Binding Name}" Command="{Binding Command}"
                                            CommandParameter="THIS SHOULD BE BINDED TO ORDERVIEWMODEL.ID"/>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
    

    视图模型:

    public class MyOrdersViewModel
    {     
        public MyOrdersViewModel()
        {
    
        }    
    
        public IList<OrderViewModel> Orders { get; set; }
    }
    
    public class OrderViewModel 
    {
        public OrderViewModel()
        {
    
        }
    
        public int Id { get; private set; }
        public DateTime OrderDate { get; private set; }
        public string Status { get; private set; }
        public DateTime LastUpdate { get; private set; }
        public string SomePerson { get; private set; }
        public int SomeNumber { get; private set; }
        public CommandViewModel ShowDetailsCommandViewModel { get; set; }
    
        private bool showItems;
    
        public bool ShowItems
        {
            get { return showItems; }
            set
            {
                showItems = value;
                RaisePropertyChanged();
            }
        }   
    
        public IList<CommandViewModel> Actions { get; private set; } 
    
        }
    }
    
    public class CommandViewModel
    {
         public CommandViewModel(string name, ICommand command)
         {
             Name = name;
             Command = command;
         }
         public string Name { get; set; }
         public ICommand Command { get; set; }
     }
    

    我需要将orderviewmodel.id绑定到orderviewmodel.actions.command参数。 我该怎么做?

    1 回复  |  直到 7 年前
        1
  •  1
  •   ASh aminescm    7 年前

    使用RelativeSource从其DataContext(OrderViewModel)中查找父DataGridRow并绑定ID:

    CommandParameter="{Binding Path=DataContext.Id, 
                               RelativeSource={RelativeSource AncestorType=DataGridRow}"/>