Carson Lee
Carson Lee

Reputation: 2893

binding event handler to custom object

How can I bind event handler to the custom object that i created?

here is my XAML

 <ListBox x:Name="ListData">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Margin="0,0,0,30">
                                    <Image VerticalAlignment="Top" HorizontalAlignment="Left" Source="{Binding Path=TileImage}" Width="175" Height="175" />
                                    <TextBlock  Margin="5" Width="200" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left" Text="{Binding Path=TileName}" FontSize="25"/>
                                </StackPanel>
                            </DataTemplate>            
                        </ListBox.ItemTemplate>
                    </ListBox>

Code Behind

    // Create collection
    ImageTiles = new ObservableCollection<ImageTile>();

    // Create each object in the collection
    ImageTile RSS= new ImageTile("RSS", "/Images/Hard.jpg");
    ImageTile test= new ImageTile("test", "/Images/Hard.jpg");
    ImageTile Exam= new ImageTile("Exam", "/Images/Hard.jpg");
    ImageTile Settings = new ImageTile("Settings", "/Images/Hard.jpg");

    ImageTiles.Add(RSS);
    ImageTiles.Add(test);
    ImageTiles.Add(Exam);
    ImageTiles.Add(Settings);

    this.ListData.ItemsSource = ImageTiles;

I would like to bind the event handler along with each ImageTile. Any idea how to do so? =)

Upvotes: 1

Views: 819

Answers (2)

KodeKreachor
KodeKreachor

Reputation: 8882

Based on your code structure I'll answer assuming you're not using MVVM or the like, however I'd definitely recommend that pattern for Silverlight development.

Nevertheless, your datasource binding would be something like:

<ListBox x:Name="ListData" ItemsSource="{Binding ImageTiles}">

</ListBox>

You could create a single generic click handler in your code behind and assign the Image's click event to that handler.

<ImageButton VerticalAlignment="Top" HorizontalAlignment="Left" Source="{Binding Path=TileImage}" Width="175" Height="175" Click="imageButton_Click" />

You could then have a method on your object responsible for redirecting to whatever necessary place for that particular image tile. Then in your code behind handler would resemble this:

private void imageButton_Click(object sender, RoutedEventArgs e)
{
    var imageTile = ((ImageButton)sender).DataContext as ImageTile;
    imageTile.RedirectSomewhere();
}

Upvotes: 1

Robaticus
Robaticus

Reputation: 23157

In your ViewModel, add a property to capture selected item (e.g.)

private ImageTile _selectedItem;
public ImageTile SelectedItem
{
     get {return _selectedItem;}
     set
     {
         if(value != _selectedItem)
         { 
              _selectedItem = value;
              RaisePropertyChanged("SelectedItem");
         }
     }
}

Then, in your XAML, bind the SelectedItem:

<ListBox ItemsSource="{Binding ImageTiles}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
</ListBox>

Use MVVM Light (or some other way) to bind the SelectionChanged event to a command, or wire up an event handler for the SelectionChanged event. In your Command (or event handler), get SelectedItem, and look at the first property (you didn't tell us what it was called, so I don't know what to call it).

Remember to make sure that SelectedItem is not null before you do anything, and, when you're done handling the command, set SelectedIndex to -1, so that they can select the same item twice, and still get the functionality to execute.

Upvotes: 1

Related Questions