Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.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# 文本框中的WPF curpos数据绑定到标签_C#_Wpf_Visual Studio 2012 - Fatal编程技术网

C# 文本框中的WPF curpos数据绑定到标签

C# 文本框中的WPF curpos数据绑定到标签,c#,wpf,visual-studio-2012,C#,Wpf,Visual Studio 2012,今天早上我遇到了一个小问题,现在我对WPF还比较陌生,我想知道如何做到这一点 在我的表单上,我有一个文本框,我有一个标签,我想将文本框的当前curpos实时绑定到该标签上(当光标移动时,表会更新,但仅针对文本框) 有人知道怎么做吗 下面是我在wpf中的文本框的一些示例代码,以及我在c#中尝试的内容 wpf: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2

今天早上我遇到了一个小问题,现在我对
WPF
还比较陌生,我想知道如何做到这一点

在我的表单上,我有一个文本框,我有一个标签,我想将文本框的当前curpos实时绑定到该标签上(当光标移动时,表会更新,但仅针对文本框)

有人知道怎么做吗

下面是我在wpf中的文本框的一些示例代码,以及我在c#中尝试的内容

wpf:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="465" Width="681">
<Grid>
    <ListBox x:Name="listbox1" HorizontalAlignment="Left" Height="405" Margin="10,10,0,0" VerticalAlignment="Top" Width="208" PreviewMouseDown="listbox1_PreviewMouseDown">
        <ListBoxItem Content="Gordon"/>
        <ListBoxItem Content="Nico"/>
        <ListBox.Resources>
            <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsMouseOver,RelativeSource={RelativeSource Self}}" 
                     Value="True">
                        <Setter Property="IsSelected" Value="True" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListBox.Resources>
    </ListBox>
    <TextBox x:Name="textbox1" HorizontalAlignment="Left" Height="405" Margin="289,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="364" SpellCheck.IsEnabled="True" Cursor="IBeam" AcceptsReturn="True" AllowDrop="True" DragEnter="textbox1_DragEnter" Drop="textbox1_Drop" PreviewMouseUp="textbox1_PreviewMouseUp"/>
    <Label x:Name="label1" Content="" HorizontalAlignment="Left" Margin="241,10,0,0" VerticalAlignment="Top"/>

</Grid>

提前感谢。

我添加了以下代码:

    private void textbox1_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        Point curpos = e.GetPosition(textbox1);
        int pos1;
        pos1 = textbox1.GetCharacterIndexFromPoint(curpos, true);
        label1.Content = pos1.ToString();
    }

我不知道我是否明白你想做什么。您想在
文本框和
标签中显示
列表框中的
SelectedItem
吗?光标在文本框中的位置指向标签我想您永远不会在
列表框1\u PreviewMouseDown
方法中找到要点您应该使用
文本框1\u PreviewMouseUp
:o)@WiiMaxx在拖动enter或鼠标悬停方法中如何?这取决于您的需要,如果您需要它,则始终如此。如果您不拖放,则应使用鼠标悬停,否则应使用拖动器
    private void textbox1_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        Point curpos = e.GetPosition(textbox1);
        int pos1;
        pos1 = textbox1.GetCharacterIndexFromPoint(curpos, true);
        label1.Content = pos1.ToString();
    }