Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在鼠标选择开始的位置绑定按钮内容_C#_Wpf - Fatal编程技术网

C# 如何在鼠标选择开始的位置绑定按钮内容

C# 如何在鼠标选择开始的位置绑定按钮内容,c#,wpf,C#,Wpf,在这里,我想绑定鼠标选择开始的按钮内容,如上图所示 但我无法解决这个问题 请帮助我。不确定我是否理解您的问题,但您是否试图在文本框中的光标位置插入按钮的内容?如果是这种情况,则需要获取文本框的插入符号索引以确定插入位置 public partial class clear : Window { public clear() { this.InitializeComponent(); } private void btn_a_C

在这里,我想绑定鼠标选择开始的按钮内容,如上图所示

但我无法解决这个问题


请帮助我。

不确定我是否理解您的问题,但您是否试图在文本框中的光标位置插入按钮的内容?如果是这种情况,则需要获取文本框的插入符号索引以确定插入位置

public partial class clear : Window
{
    public clear()
    {
        this.InitializeComponent();         

    }

    private void btn_a_Click(object sender, RoutedEventArgs e)
    {
        txt_display.Text += btn_a.Content.ToString();
        txt_display.SelectionStart = txt_display.Text.Length;
        txt_display.Focus();
    }

    private void btn_b_Click(object sender, RoutedEventArgs e)
    {
        txt_display.Text += btn_b.Content.ToString();
        txt_display.SelectionStart = txt_display.Text.Length;
        txt_display.Focus();
    }
}

你应该试着更具体地说明你想做什么。假设您正好有两个按钮,并且希望它们的内容与文本框插入符号位置右侧的第一个和第二个字母同步,您可以为文本框SelectionChanged事件创建一个事件,如下所示:

private void btn_a_Click(object sender, RoutedEventArgs e)
{
    var caretIndex = txt_display.CaretIndex;
    txt_display.Text =txt_display.Text.Insert(caretIndex, btn_a.Content.ToString());
    txt_display.SelectionStart = txt_display.Text.Length;
    txt_display.Focus();
}

在本例中,如果您有一个文本框,其中包含文本“Hello World!”,插入符号位置在d的左侧,则按钮将分别显示文本“d”和“!”。

Hoang,你是对的。我已经得到了我需要的。我只是忘了提一件事。在我绑定文本后,光标会移动到文本框的右角。但是光标选择必须在我绑定的文本之后开始,直到我更改光标选择。请帮我…在这种情况下,只需将插入符号设置为当前插入符号索引之后的一个<代码>var caretIndex=txt_display.caretIndex;txt_display.Text=txt_display.Text.Insert(caretIndex,btn_a.Content.ToString());txt_display.Focus();txt_display.CaretIndex=CaretIndex+1Hoang请查看这些链接。和。我在这里发布了一个问题。我希望你是解决这个问题的唯一合适的人。。。请不要发烧。。
private void btn_a_Click(object sender, RoutedEventArgs e)
{
    var caretIndex = txt_display.CaretIndex;
    txt_display.Text =txt_display.Text.Insert(caretIndex, btn_a.Content.ToString());
    txt_display.SelectionStart = txt_display.Text.Length;
    txt_display.Focus();
}
    private void txtbox_SelectionChanged(object sender, RoutedEventArgs e)
    {
        if (btn != null)
        {
            string letters = txtbox.Text.Substring(txtbox.SelectionStart);

            if (letters.Length > 0)
                btn.Content = letters[0];

            if (letters.Length > 1)
                btn2.Content = letters[1];
        }
    }