Note that there are some explanatory texts on larger screens.

plurals
  1. PODeleting a ListBoxItem from a button on it's data template on WPF and C#
    primarykey
    data
    text
    <p>I've been writing an applicaton on WPF. It has a ListBox bound to a ObservableCollection&lt;> of a custom class. The data is displayed using a dataTemplate wich has a DELETE button that is suposed to remove the item of the ObservableCollection&lt;>. </p> <p>The data source collection is defined as shown:</p> <pre><code>public class SellItem : INotifyPropertyChanged { private string _name, _code, _details; public SellItem Self { get { return this; } } public string Name { get { return _name; } set { _name = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Name")); } } public string Code { get { return _code; } set { _code = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Code")); } } public string Details { get { return _details; } set { _details = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Details")); } } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; #endregion } public class ItemCollection : ObservableCollection&lt;SellItem&gt; { static public ItemCollection theCollection = null; public ItemCollection() { theCollection = this; } } </code></pre> <p>The xaml code looks like this:</p> <pre><code>&lt;Window x:Class="NALT_WPF.SaleWindow" ... xmlns:local="clr-namespace:NALT_WPF" Title="Test" Height="494" Width="838"&gt; &lt;Window.Resources&gt; &lt;ObjectDataProvider x:Key="theItemCollection" ObjectType="{x:Type local:ItemCollection}"/&gt; &lt;/Window.Resources&gt; &lt;Window.CommandBindings&gt; &lt;!--Using command binding--&gt; &lt;CommandBinding Command="Delete" Executed="CommandBinding_Executed_RemoveAll" /&gt; &lt;/Window.CommandBindings&gt; &lt;Grid&gt; &lt;Grid.Resources&gt; &lt;DataTemplate x:Key="ProductListItemTemplate"&gt; &lt;Grid&gt; ... &lt;!-- This is the button on data template to remove the item from 'ItemCollection' --&gt; &lt;Button Name="btRemoveAllItems" Content="X" Command="Delete" CommandParameter="{Binding Self}"&gt; &lt;/Button&gt; ... &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/Grid.Resources&gt; ... &lt;ListBox Grid.Row="1" Margin="10" Name="lstProducts" ItemTemplate="{StaticResource ResourceKey=ProductListItemTemplate}" ItemsSource="{Binding Source={StaticResource theItemCollection}}" HorizontalContentAlignment="Stretch"&gt; &lt;/ListBox&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>And on the main window C# code:</p> <pre><code>public partial class MainWindow : Window { public SaleWindow() { InitializeComponent(); } ... private void CommandBinding_Executed_RemoveAll(object sender, ExecutedRoutedEventArgs e) { // this is the code executed when DELETE button on data template is pressed. // I was thinking to remove the item here MessageBox.Show("Removing, proceed? ..."); //... ... //... } } </code></pre> <p>How can I do that? To remove the item from the collection on the listBox of wich the button on the template belongs to. The item is not always the selected one.</p> <p>I appreciate any help :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload