在另一个控件的事件中访问其他xaml控件数据

在另一个控件的事件中访问其他xaml控件数据

问题描述:

我有下面的列表框,我绑定数据从一个类(SavedDataClass)defined.When用户点击链接“更新”我想访问其他成员的整个数据SavedDataClass的特定实例。是否可以像这样访问数据?我的意思是如何当它的成员之一被调用访问列表框项实例等xxaml控制数据..在另一个控件的事件中访问其他xaml控件数据

 <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; } 
} 

更新链接点击将事件处理程序如下:

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

你的数据对象(SaveDataClass)应该在DataContext属性中点击按钮 – jure 2013-05-02 16:21:25

+0

我有属性“public string Update {get; set;}”绑定到更新按钮。 – krrishna 2013-05-02 16:23:10

在这些情况下, FrameworkElementTag属性用于传输位于您要绑定的DataContext内的基础数据对象。您的代码将看起来有点像这样:

<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> 

注意,在第二个超链接按钮,我添加了Tag属性。您的代码隐藏看起来有点像这样:

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... 
} 

希望这会有所帮助。

+0

yup.It works.Many谢谢。 – krrishna 2013-05-02 16:33:57