2016-06-14 61 views
-1

我有一個xaml datagrid模板在我的WPF應用程序綁定/從一個SharePoint站點的數據。每行都有一個帶有工具提示的按鈕,文本從該連接動態加載。當用戶點擊按鈕時,它會將工具提示文本複製到Windows剪貼板。就在最近,我現有的代碼停止工作,似乎無法再獲得動態加載的工具提示文本,無法在代碼中使用。我已經排除了剪貼板,因爲我可以手動設置文本並將其複製到剪貼板,並且仍然可以在UI中看到動態加載的文本,因此它不能作爲連接。下面是我如何使用獲得的點擊事件文本:c#wpf工具提示,無法獲得代碼背後的工具提示文本

var buttonTemplate = ((Button)sender).ToolTip; 
var buttonTTtext = ((TextBlock)buttonTemplate).Text; 
System.Windows.Clipboard.SetText(buttonTTtext.ToString()); 

這裏是我使用在DataGrid模板中的XAML:

<mui:ModernButton Click="someButton_Click" Tag="{Binding Path=MyTemplate}" > 
<mui:ModernButton.ToolTip> 
<TextBlock Text="{Binding Path=template}" TextWrapping="Wrap" /> 
</mui:ModernButton.ToolTip> 
</mui:ModernButton> 

回答

-1

感謝您的幫助...的提示文本在按鈕的DataContext中。我用這段代碼解決了我的問題:

Button btn = (Button)sender; 
var dc = btn.DataContext.GetType().GetProperty("template").GetValue(btn.DataContext, null); 
+0

我想這是一個社區,如果有人試圖幫助你,並且你自己找到答案,他們會刪除他們的評論,並降低你的問題和答案。 (搖搖頭) – Russell