我在我的視圖模型中有一個遊標位置屬性,它決定了視圖中文本框中游標的位置。我如何將cursorposition屬性綁定到文本框內光標的實際位置。處理文本框內的光標
0
A
回答
1
恐怕你不能...至少,不是直接的,因爲TextBox控件上沒有「CursorPosition」屬性。
您可以通過在代碼隱藏中創建DependencyProperty,綁定到ViewModel以及手動處理光標位置來解決該問題。以下是一個示例:
/// <summary>
/// Interaction logic for TestCaret.xaml
/// </summary>
public partial class TestCaret : Window
{
public TestCaret()
{
InitializeComponent();
Binding bnd = new Binding("CursorPosition");
bnd.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(this, CursorPositionProperty, bnd);
this.DataContext = new TestCaretViewModel();
}
public int CursorPosition
{
get { return (int)GetValue(CursorPositionProperty); }
set { SetValue(CursorPositionProperty, value); }
}
// Using a DependencyProperty as the backing store for CursorPosition. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CursorPositionProperty =
DependencyProperty.Register(
"CursorPosition",
typeof(int),
typeof(TestCaret),
new UIPropertyMetadata(
0,
(o, e) =>
{
if (e.NewValue != e.OldValue)
{
TestCaret t = (TestCaret)o;
t.textBox1.CaretIndex = (int)e.NewValue;
}
}));
private void textBox1_SelectionChanged(object sender, RoutedEventArgs e)
{
this.SetValue(CursorPositionProperty, textBox1.CaretIndex);
}
}
0
您可以使用CaretIndex屬性。然而,它不是一個DependencyProperty,也沒有實現INotifyPropertyChanged,所以你不能真正綁定它。
相關問題
- 1. 粘貼文本框中光標處的文本行
- 2. 在光標處插入文本框中的文本
- 3. jQuery的文本框焦點()處的任何地方光標
- 4. 如何添加文本框的文本標記和處理事件處理
- 5. VB.NET文本框跟隨鼠標光標
- 6. 甲骨文PRO * C:取處理光標
- 7. 在文本框中的光標對齊
- 8. 文本框事件處理
- 9. wpf文本框光標滾動
- 10. 將光標懸停在文本框上
- 11. 僅當光標焦點位於文本框內時纔有Javascript
- 12. 添加光標位置的文本的文本框在vb.net
- 13. 文本框始終把光標放在文本的開頭
- 14. 文本框和光標高度中的文本對齊
- 15. SWT光標文本
- 16. 如何爲光標在usercontrol內的文本框內按下「Enter」設置事件
- 17. 如何在光標觸及相關標籤時顯示文本框的內容?
- 18. 如何處理動態表內的動態文本框?
- 19. 處理遊戲光標,而不是Windows光標
- 20. 光標文本的新行
- 21. jQuery文本框光標到文本結尾?
- 22. 如何在文本框當前光標中粘貼文本?
- 23. 在UWP文本框中垂直居中文本和光標
- 24. grep - 處理引號內的文本
- 25. 甲骨文:內光標
- 26. Silverlight 4 - 如何讓文本框中的光標處於活動狀態?
- 27. 加快〜50GB CSV的光處理文件
- 28. javafx:如何處理項目(文本框,標籤等)?
- 29. C#:文本框LostFocus事件處理
- 30. 如何處理文本框驗證?
感謝您的回覆托馬斯。我會試試看,並會回覆你。 – deepak 2009-06-11 10:23:48