TryNCode
TryNCode

Reputation: 441

Access button in ListBoxItem in Silverlight

Hey everyone. I'm new to Silverlight and would like to access a button control that is part of a ListBoxItem. However, I'm not quite sure how to do this. My XAML is:

<DataTemplate x:Key="ItemTemplate2">
            <Grid Height="51">
                <TextBlock x:Name="tbName" Text="{Binding Property1}" Margin="0,0,98,0" d:LayoutOverrides="Height" Grid.ColumnSpan="2" HorizontalAlignment="Left" VerticalAlignment="Center"/>
                <Button x:Name="btnInfo"  HorizontalAlignment="Right" Margin="0,-11,0,0" Width="87" Height="54" VerticalAlignment="Top" Background="#FF8EC1D2" BorderBrush="#FF8EC1D2" BorderThickness="0">
                    <Image Source="../Images/btnNameImage.png" Stretch="None"/>                 
                </Button>
            </Grid>
        </DataTemplate>

How can I access the "btnInfo" from the code-behind?

Thanks for any help

Upvotes: 1

Views: 612

Answers (2)

Brandon Copeland
Brandon Copeland

Reputation: 358

This doesn't directly answer your question of programatically accessing a data template, but how about Binding?

Upvotes: 0

luke
luke

Reputation: 14788

you wont be able to access it directly from code behind, but you should be able to do what you want by registering an event with it. Add a Loaded event to the button then in your codebehind define the corresponding method.

protected btninfo_Loaded(object sender, EventArgs e)
{
    Button btnInfo = (Button) sender;
    //do whatever you need to do
}

obviously you can do the same thing to handle click events or whatever you want.

Upvotes: 1

Related Questions