访问其他控件的事件中的其他xaml控件数据

时间:2013-05-02 16:13:57

标签: c# wpf xaml binding datacontext

我有下面的列表框,我从一个类(SavedDataClass)定义绑定数据。当用户点击链接“更新”时,我想访问该特定SavedDataClass实例的其他成员的整个数据。这是可能的像那样访问数据?我的意思是当调用其成员之一时,如何访问列表框项实例中的其他xxaml控件数据。

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

1 个答案:

答案 0 :(得分:1)

在这些情况下,Tag的{​​{1}}属性用于传输驻留在您绑定的FrameworkElement内的基础数据对象。您的代码看起来有点像这样:

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

希望这有帮助。