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
    }

Answers


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.


Need Your Help

Is there a cleaner way to replicate an unordered_map with multi-type values in c++(11)

c++ types unordered-map

The basic idea is to get a unordered_map that stores values of different types. What I am trying to do is to create an easy accessible object to a OpenGL Uniform Buffer Object. The end product would

Devise how to associate current user to post?

ruby-on-rails ruby-on-rails-3 devise

How do I associate my current user that is signed in to my post on create?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.