2012-04-11 67 views
0

我有一个来自sharepoint的列表,我从这个列表中收集了一个超链接。 因为我想我的文本框就像一个超链接我已经在mousedown上添加了一个事件来打开这个超链接,我关心的是如何在发件人的代码隐藏中收集这个超链接。 目前,我刚刚在工具提示中隐藏了这个超链接,也许我可以以不同的方式管理这些超链接,任何建议都将得到重视。 我的观点到目前为止,我不知道如何在后面的代码中得到这个工具提示。 感谢TextBlock MouseDown URL - 如何让我的代码位于我的代码后面?

我的XAML代码:

<ListBox Name="ListboxTips" ItemsSource="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Grid> 
        <StackPanel Orientation="Horizontal"> 

         <Image Source="{Binding Path=Picture}" Height="20"></Image> 
         <TextBlock MouseDown="TextBlock_MouseDown_URL" TextDecorations="Underline" 
            Margin="10,10,20,10" Width="160" TextWrapping="Wrap" 
            Text="{Binding Path=TitleTip}" 
            ToolTip="{Binding Path=URL}"/> 
        </StackPanel> 

       </Grid> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

我后面的代码:

  foreach (SPSClient.ListItem item in TipsList) 
      { 

       var tips = new Tips(); 
       tips.TitleTip = item.FieldValues.Values.ElementAt(1).ToString(); 
       tips.App = item.FieldValues.Values.ElementAt(4).ToString(); 

       // get the Hyperlink field URL value 
       tips.URL = ((FieldUrlValue)(item["LinkDoc"])).Url.ToString(); 

       //should collect the description of the url 
       //tips.URLdesc = ((FieldUrlValue)(item["LinkDoc"])).Description.ToString(); 
       tips.Picture = item.FieldValues.Values.ElementAt(4).ToString(); 

       colTips.Add(tips); 
      } 
      ListboxTips.DataContext = colTips; 

....

private void TextBlock_MouseDown_URL(object sender, System.Windows.Input.MouseButtonEventArgs e) 
    { 
     //string test = (ToolTip)(sender as Control).ToString(); 
     System.Diagnostics.Process.Start("http://www.link.com"); 
     //System.Diagnostics.Process.Start(test); 
    } 

非常感谢,

回答

0

你可以访问属性直接。它不是优雅的,但会工作!

private void TextBlock_MouseDown_URL(object sender, MouseButtonEventArgs e) 
{ 
    TextBlock txtBlock = sender as TexBlock; 
    // just access the property 
    string url = txtBlock.ToolTip as string; 
} 

一个更好的方法可能是使用一个ButtonHyperlink或东西暴露了Command,这样就可以在执行您想要的操作您的视图模型绑定的“点击”动作命令执行。

0

通常你会粘贴任何你想要变成Tag属性的地方的数据。

<TextBlock .. Tag="{Binding Path=URL}" /> 

这是很容易检索作为公共财产:

private void TextBlock_MouseDown_URL(object sender, System.Windows.Input.MouseButtonEventArgs e) 
{ 
    var tb = sender as TextBlock; 
    if(tb != null) 
    { 
     var neededUrl = tb.Tag; 
    } 
}