0
votes

So I got in WPF an ListBox where I got an Button in my ListBoxItem through an DataTemplate. Im adding that Button Like this to my Item:

 <ListBox Grid.Column="1" BorderBrush="Black" Margin="15,20,10,15" MinHeight="25" Name="tbxFiles"
                 VerticalAlignment="Stretch"
             ItemsSource="{Binding Items}"
             SelectionMode="Multiple">
                    <ListBox.Resources>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="OverridesDefaultStyle" Value="true" />
                            <Setter Property="SnapsToDevicePixels" Value="true" />
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="ListBoxItem">
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="250"/>
                                                <ColumnDefinition Width="50"/>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>

                                            <TextBlock x:Name="ListText" Text="{Binding}" Grid.Column="0"/>
                                            <RadioButton Grid.Column="1"  Content="TF"  />
                                            <RadioButton Grid.Column="2"  Content="AF" />
                                            <ComboBox Grid.Column="3" Text="Periode"  />
                                            <Button Grid.Column="4"  Click="RemoveMark_Click"  Content="Delete" />
                                        </Grid>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ListBox.Resources>
                </ListBox>

Every ListBox Item that gonna be created hass an deleteButton now and when I press this Button, this Code gonna be startet to Delete this Item:

        PeriodeCombo.Items.Clear();
        string required = tbxFiles.SelectedItems.Cast<string>().Aggregate((a, b) => a + b);

         required = tbxFiles.SelectedItems.Cast<string>().Distinct()
                         .Aggregate((a, b) => a + b);

         required = tbxFiles.SelectedItems.Cast<string>().Distinct()
                          .Aggregate((a, b) => a + ";" + b);

        string[] words = required.Split(';');
        foreach (var word in words)
        {
            temp1.Add(word);
        }

        for (int i = 0; i < temp1.Count; i++)
        {
            path.Remove(temp1[i]);
        }

            path.Remove(required);
        tbxFiles.Items.Remove(tbxFiles.SelectedItem);

        while (tbxFiles.SelectedItems.Count > 0)
        {
            tbxFiles.Items.Remove(tbxFiles.SelectedItems[0]);
        }

And this Code remove only these item I clicked on that they are checked. But what I want to do now is that I can delete the Items without selecting by clicking the delete Button in that the Item are. So That when I click the delete button the Item gonna get deletet without to click on that item. How did I kneed to change my Code to get this working?

1

1 Answers

0
votes

In RemoveMark_Click the first argument (typically called sender and of type object) should by the Button you clicked on, and its DataContext should be your collection item to remove.

So change RemoveMark_Click to sth like:

void RemoveMark_Click(object sender, RoutedEventArg args)
{
   //TODO add null checks a/o exception handling
   var uiElement = (Button)sender;
   var dataItem = uiElement.DataContext;

   tbxFiles.Items.Remove(dataItem);
}

Generally spoken, for a button, it is more common and good practice to use a Command to execute the desired action, because then you can put the magic in the ViewModel. Also, in order to have a special layout for a listboxitem setting the datatemplate a/o the itemcontainerstyle of the listbox will typically suffice, you don't need to override the control template, but of course you can and it will work.