2012-04-11 64 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; 
    } 
}