ItemsControl button event

Jan 20, 2011 at 10:41 PM

Hi I'm using a ItemsControl to show some buttons from an object in the ViewModel, that have an Image and a label, I've added a Mesage.Attach event to the button, the problem is that all the buttons are binded to the same Show() method and I don't know what is the specific view I have to load because actually I don't know which button was clicked.

If you could show me an example about to get the clicked button to redirect to a specific view, it will be very appreciated.

The code I'm using is:

 

 

MainView.xaml


<tc:DockPanel>

            <ContentControl x:Name="ActiveItem" tc:DockPanel.Dock="Top" />

            <StackPanel Orientation="Horizontal" Margin="5" >
                <ItemsControl Margin="10" ItemsSource="{Binding Wood.Items}" tc:DockPanel.Dock="Bottom"
			              HorizontalAlignment="Center">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Button cal:Message.Attach="Show"
						        Margin="8">
                            <StackPanel>
                                <StackPanel>
                                <Image Source="{Binding Img}" Height="100" />
                                </StackPanel>
                                    <TextBlock Margin="3,3,3,0" FontSize="12" HorizontalAlignment="Center" Text="{Binding Path=Title}" TextAlignment="Center" />
                            </StackPanel>
                            </Button>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </tc:DockPanel>


MainViewModel.cs

public class MainWindowViewModel : Conductor<object>
    {
        WoodType wood = new WoodType
                {
                    Items = new List<Item> 
                    {
                        new Item { Title = "Pencil" , Img = new BitmapImage(new Uri("../Images/WoodenPencil.png",UriKind.Relative)) },
                        new Item { Title = "Wooden Spoon" , Img = new BitmapImage(new Uri("../Images/WoodenSpoon.png",UriKind.Relative))}
                    }
                };

        public WoodType Wood
        {
            get { return wood; }
            set { wood = value; }
        }
        

        public MainWindowViewModel()
        {
        }

     //I'm not getting the clicked button to activate the specific view
        public void Show()
        {
            ActivateItem(new NewViewModel());
        }
}


Item.cs

    public class Item
    {
        public string Title { get; set; }
        public BitmapImage Img { get; set; }
    }

 

 

Coordinator
Jan 21, 2011 at 3:11 AM

Change your message to this: 

         cal:Message.Attach="Show($dataContext)"
And change your action to this:
        public void Show(Item item)
        {
            ActivateItem(new NewViewModel());
        }