views:

47

answers:

1

Hi, I'm a bit new to WPF. I am working on a list UI where each item in the list will have a set of corresponding buttons to operate on that particular data item.

Coming from a web background, I normally would have bound the value into a hidden element in that particular list item or something. However, I just need to find the corresponding technique in this WPF world :-)

+1  A: 

The most common technique is to use templates. Please consider using my example of a templated ListItem (for example ListBoxItem):

   <ListBox>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Button Command={Binding Path=YourCommand} Text="Dynamic Button 1" />
                    <Button Command={Binding Path=YourSecondCommand} Text="Dynamic Button 2" />
                <StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Please feel free to ask if you have any questions/ideas.

Piotr Justyna
Thanks for your answer. I see ... ok, so how would I a) define YourCommand so that the binding above routes it correctly, and b) in that command, how do I reference the list item's data object so that I can operate on it?
Joel Martinez
You will have to prepare appropriate DataContext for your View. This sounds scary at the beginning, but very soon you will find that extremely easy. Please take a look on this great article covering bound commands (using MVVM design pattern) http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx Author used TreeView, but you can use Listbox as well with no hierarchical data templates. It seems to be complicated at first glance, but to use full potential of WPF you'll have to get familiar with bindings. Please give me a shout if you nedd help!
Piotr Justyna