【问题标题】:Deleting a ListBoxItem from a button on it's data template on WPF and C#从 WPF 和 C# 中的数据模板上的按钮中删除 ListBox 项
【发布时间】:2011-06-08 21:36:20
【问题描述】:

我一直在写一个关于 WPF 的应用程序。它有一个绑定到自定义类的 ObservableCollection 的 ListBox。数据使用具有删除按钮的 dataTemplate 显示,该按钮应该删除 ObservableCollection 的项目。

数据源集合定义如下:

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<SellItem>
{
    static public ItemCollection theCollection = null;

    public ItemCollection()
    {
        theCollection = this;

    }
}

xaml 代码如下所示:

<Window x:Class="NALT_WPF.SaleWindow"
    ...
    xmlns:local="clr-namespace:NALT_WPF"
    Title="Test" Height="494" Width="838">
<Window.Resources>
    <ObjectDataProvider x:Key="theItemCollection" ObjectType="{x:Type local:ItemCollection}"/>
</Window.Resources>
<Window.CommandBindings>
    <!--Using command binding-->
    <CommandBinding Command="Delete" Executed="CommandBinding_Executed_RemoveAll" />
</Window.CommandBindings>
<Grid>
    <Grid.Resources>
        <DataTemplate x:Key="ProductListItemTemplate">
            <Grid>
                ...
                        <!-- This is the button on data template to remove the item from 'ItemCollection' -->
                        <Button Name="btRemoveAllItems" Content="X" 
                                Command="Delete" CommandParameter="{Binding Self}">
                        </Button>
                ...
            </Grid>
        </DataTemplate>
    </Grid.Resources>
    ...
    <ListBox Grid.Row="1" Margin="10" Name="lstProducts" 
                         ItemTemplate="{StaticResource ResourceKey=ProductListItemTemplate}"
                         ItemsSource="{Binding Source={StaticResource theItemCollection}}"
                         HorizontalContentAlignment="Stretch">
    </ListBox>
</Grid>

并在主窗口 C# 代码:

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? ...");
         
        //...  ... 

        //...
        
    }
}

我该怎么做?从模板上的按钮所属的 listBox 上的集合中删除项目。该项目并不总是被选中的。

【问题讨论】:

    标签: listbox binding datatemplate


    【解决方案1】:

    类似这样的:

    private void CommandBinding_Executed_RemoveAll(object sender, ExecutedRoutedEventArgs e) 
    {
        SellItem sellItem = (SellItem) e.Parameter;
        var itemsCollection = (ItemCollection)lstProducts.ItemsSource;
        itemsCollection.Remove(sellItem);
    }
    

    虽然,看起来不太对劲。您应该考虑以 MVVM 样式重写此代码。这样,您将拥有一个拥有产品列表的视图模型,并且该视图模型将定义一个删除命令,该命令将从集合中删除该项目。

    还有一个提示。您无需定义 Self 属性即可绑定到它。您可以使用不带任何参数的绑定扩展:

    <Button Name="btRemoveAllItems" Content="X" 
            Command="Delete" CommandParameter="{Binding}">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-05
      • 1970-01-01
      • 2014-10-12
      • 2020-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多