I have problem with binding in XAML. I have a DataGrid which has a ListBox of Buttons inside. I need to bind command parameter of the buttons to DataGrid's items Ids. I have tried using RelativeSource, naming hidden column with Id and trying to bind column datacontext but nothing is working. I've also tried to bind to SelectedItem in DataGrid, command works fine then but CanExecute does not as each button is updated according to selected row, not the row it's actualy in. Here Is My code:
View:
<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>
ViewModels:
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; }
}
I need to bind OrderViewModel.Id to OrderViewModel.Actions.Command parameter. How can I do that?