开发者

How to include buttons and map their events within a ListBox?

I have a concept question on how to have a dynamic list displayed by a list box. Within the list box, the items are held within a Stackpanel. Within the stack panel is a string and a button. If I click that button, that entity / item (both the button and the string) is removed from th开发者_如何学Pythone list box.

1) How do I create new buttons based on the number of entities in the list?

2) How do I connect the idea that this button is related to this item in the listbox?

3) Is this even possible with binding?

Sorry, I am still new to WPF but starting to see that it is quite powerful.

If you can also provide a brief / short example, or direct me to a link that is similar to this, it would be greatly appreciated. Thanks so much! :)


This is a simple example of how you can do this. First, the source class for the ListBox. A simple class that implements the INotifyPropertyChanged interface and one string property.

public class MyClass : INotifyPropertyChanged
{
    public MyClass(string myString)
    {
        MyString = myString;
    }
    private string m_myString;
    public string MyString
    {
        get
        {
            return m_myString;
        }
        set
        {
            m_myString = value;
            OnPropertyChanged("MyString");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

The MainWindow has an ObservableCollection of MyClass. We set the DataContext for the Window to itself (this), this will make the ListBox inherit the DataContext so we can Bind its ItemsSource to MyClasses.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        MyClasses = new ObservableCollection<MyClass>();
        MyClasses.Add(new MyClass("My String 1"));
        MyClasses.Add(new MyClass("My String 2"));
        MyClasses.Add(new MyClass("My String 3"));
        this.DataContext = this;
    }

    public ObservableCollection<MyClass> MyClasses
    {
        get;
        private set;
    }
}

In xaml we have the ListBox that's binding to MyClasses. The ItemTemplate is for each ListBoxItem in the ListBox, each item binding to its instance of MyClass, presenting the MyString property and a "Remove Button". The DataContext for each ListBoxItem will be an instance of MyClass.

<ListBox ItemsSource="{Binding MyClasses}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding MyString}" Margin="6"/>
                <Button Content="Remove"
                        Click="RemoveListBoxItem_Click"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Finally, we add the code behind EventHanlder for RemoveListBoxItem_Click in MainWindow.xaml.cs.

private void RemoveListBoxItem_Click(object sender, RoutedEventArgs e)
{
    // The clicked Button
    Button button = sender as Button;
    // The DataContext of the Button will be its instance of MyClass
    MyClass selectedItem = button.DataContext as MyClass;
    if (selectedItem != null)
    {
        // Remove the MyClass item from the collection
        MyClasses.Remove(selectedItem);
    }
}


I guess you'll need to change the control template and or the content template of the control. Have a look at the following links http://mark-dot-net.blogspot.com/2007/07/creating-custom-wpf-button-template-in.html http://msdn.microsoft.com/en-us/magazine/cc163497.aspx

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜