2013-02-26 23 views
0

我正在Silverlight中工作。 我有一個DataGrid內的文本框,我的頁面上有一個按鈕,其功能是用括號括住選定的文本。例如。讓我的文本框中的文本是「Hello World」,現在當我的數據網格進入編輯模式時,我想從該文本框中選擇一些文本。讓它成爲「世界」。現在我已經選擇了「世界」,當我點擊按鈕時,文本框中的輸出應該是「Hello(World)」。 但問題是,當我選擇文本並單擊按鈕,文本框失去焦點和文本保持不變。並在案件如果我集中文本框,然後按鈕不會被點擊。更改按鈕上datagrid的數據模板中定義的文本框中的文本單擊

請任何人都可以提出解決方案。

回答

0

您可以使用客戶端事件嗎? 你可以看一看JQuery的插入符插件: http://www.examplet.buss.hk/jquery/caret.php

// Get start position in textbox box with id="textbox1" 
$("#textbox1").caret().start 

// Get end position in textbox 
$("#textbox1").caret().end 

// Get selected text in textbox 
$("#textbox1").caret().text 

也有簡單的JavaScript解決方案:

<script language=javascript> 
function getSelText() 
{ 
var txt = ''; 
    if (window.getSelection) 
    { 
     txt = window.getSelection(); 
    } 
    else if (document.getSelection) 
    { 
     txt = document.getSelection(); 
    } 
    else if (document.selection) 
    { 
     txt = document.selection.createRange().text; 
    } 
    else return; 
document.aform.selectedtext.value = txt; 
} 
</script> 
<form> 
    <input type="button" value="Get selection" onmousedown="getSelText()"> 
    <form name=aform > 
    <textarea name="selectedtext" rows="5" cols="20"></textarea> 
</form> 

來源:http://www.codetoad.com/javascript_get_selected_text.asp

+0

其實我需要在代碼變化背後並不能使用JavaScript。 – user2091061 2013-02-26 09:40:43

+0

你可以使用textBox1.SelectionStart和textBox1.SelectionLength嗎? 您可以使用這些與LostFocus事件臨時存儲的值 – 2013-02-26 10:02:36

+0

是的..我已經存儲選定的文本,但問題是在文本中進行更改,更改不反映在DataGrid中。 – user2091061 2013-02-26 10:14:49

相關問題