I think it’s time to ask for help! I have an ItemsControl that lists UserControls (called CompoundCardControl):
<ScrollViewer Grid.Row = "1" VerticalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding Method.Compounds}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<control:CompoundCardControl/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>type here
The UserControl contains a button that is suppose to remove the item from the ListView (and the ViewModel):
<Grid Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="25"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Margin="5,0,0,0" Text="{Binding Name}" />
<TextBlock Grid.Column="1" Margin="5,0,0,0" Foreground="Gray" FontSize="10" VerticalAlignment="Center" Text="RT"/>
<TextBlock Grid.Column="2" Margin="5,0,0,0" Text="{Binding RetentionTime , StringFormat= '{0} minutes'}" />
<Button Grid.Column="3" HorizontalAlignment="Right" Style="{StaticResource menuBarButton}"
Command="{Binding DataContext.DeleteCompoundCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"
CommandParameter="{Binding Compound}">
<Image Width="12" Height="12" Source="/Resources/Icons/Delete.png"/>
</Button>
</Grid>
This is my ViewModel for my parent window (but it obviously doesn’t work)”:
public class MethodDialogViewModel : ViewModelBase
{
//properties
public DialogModeEnum DialogMode { get; private set; }
public string WindowTitle { get { return DialogMode == DialogModeEnum.Add ? "New method" : "Edit method"; } }
public string ButtonText { get { return DialogMode == DialogModeEnum.Add ? "Add" : "Update"; } }
public Method Method { get; set; }
public MethodCompound Compound { get; set; } = new MethodCompound();
//constructor
public MethodDialogViewModel(Method method, DialogModeEnum dialogMode)
{
DialogMode = dialogMode;
Method = method;
}
//commands
public RelayCommand DeleteCompoundCommand => new(execute => DeleteCompound(Compound));
private void DeleteCompound(MethodCompound methodCompound)
{
//Logic not implimented. I just want to see if I can get the object "methodCompound"
if (methodCompound != null)
{
MessageBox.Show(methodCompound.ID);
}
else
{
MessageBox.Show("Please select a compound.", "Delete compound", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
Basically I want to pass the Item (actually, the underlying object) in the ItemsControl to the ViewModel. How do I do that? Or am I totally botching this?