krrishna
krrishna

Reputation: 2078

Access other xaml controls data in another control's event

I have below listbox to which i am binding data from a class(SavedDataClass) defined.When user clicks on link "Update" i want to access the entire data of other members of that particular instance of SavedDataClass .Is that possible to access the data like that ? I mean how to access other xxaml controls data in a listbox item instance when one of its member is invoked ..

        <ListBox x:Name="lstAreaDetails"  Grid.Row="1" Margin="0,10,0,0"  >
                        <ListBox.ItemTemplate >
                            <DataTemplate >
                                <StackPanel >

                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Foreground="White" Name="MyDateTime" Text="{Binding MyDateTime}"></TextBlock>
                                    </StackPanel>                                        
                                    <StackPanel >                                           
                                        <HyperlinkButton Content="{Binding SavedName}" Name="lnkSAvedName"  Click="HyperlinkButton_Click_1"   />
                                        <HyperlinkButton  Content="{Binding Update}" Name="lnkUpdate" Click="lnkUpdate_click"/>
                                    </StackPanel>
                                    <TextBlock   Text="{Binding ResAddress}" Name="txtResAddress" TextWrapping="Wrap" ></TextBlock>
                                    <TextBlock >OtherDetails:</TextBlock>
                                    <TextBlock  Text="{Binding Area}" Name="txtArea" TextWrapping="Wrap"></TextBlock>
                                </StackPanel>
                            </DataTemplate>

                        </ListBox.ItemTemplate>
                    </ListBox>

 public class SavedDataClass
{
    public string MyDateTime { get; set; }
    public string SavedName { get; set; }
    public string Update{ get; set; }
    public string ResAddress { get; set; }
    public string Area{ get; set; }
    public string OptionalAddressLine1{ get; set; }
    public string OptionalAddressLine2{ get; set; }
}

update link click will have below event handler:

 private void lnkUpdate_click(object sender, RoutedEventArgs e)
    {
      //here I want to access other controls data e.g. MyDateTime,SavedName,ResAddress,Area,OptionalAddressLine1,OptionalAddresLine2
    }

Upvotes: 0

Views: 229

Answers (1)

feO2x
feO2x

Reputation: 5728

In these situations, the Tag property of FrameworkElement is used to transport the underlying data object that resides within the DataContext you're binding to. Your code would look somewhat like this:

<ListBox x:Name="lstAreaDetails" Grid.Row="1" Margin="0,10,0,0">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Foreground="White" Name="MyDateTime" Text="{Binding MyDateTime}"></TextBlock>
                </StackPanel>                                        
                <StackPanel >                                           
                    <HyperlinkButton Content="{Binding SavedName}" Name="lnkSAvedName"  Click="HyperlinkButton_Click_1"   />
                    <HyperlinkButton  Tag="{Binding}" Content="{Binding Update}" Name="lnkUpdate" Click="lnkUpdate_click"/>
                </StackPanel>
                <TextBlock   Text="{Binding ResAddress}" Name="txtResAddress" TextWrapping="Wrap" ></TextBlock>
                <TextBlock >OtherDetails:</TextBlock>
                <TextBlock  Text="{Binding Area}" Name="txtArea" TextWrapping="Wrap"></TextBlock>
            </StackPanel>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

Notice that on the second hyperlink button, I added the Tag attribute. Your code-behind would look somewhat like this:

private void lnkUpdate_click(object sender, RoutedEventArgs e)
{
    var hyperlinkButton = sender as HyperlinkButton;
    if (hyperlinkButton == null)
        return;

    var savedDataClass = hyperlinkButton.Tag as SavedDataClass;
    // Do whatever you want with the saved data class instance here...
}

Hope this helps.

Upvotes: 1

Related Questions