如何在wpf中突出显示当前键入的文本框文本

时间:2012-06-13 10:02:17

标签: c# wpf

public partial class MultiTexbox_2 : Window
{
    Control texbox_full_details = null;      //get all textbox property and method in when gotfocused
    Control button_full_details;             //get all button property and method in when click event
    Button keyboard_button;     //behave like button

    public MultiTexbox_2()
    {
        this.InitializeComponent();

        // Insert code required on object creation below this point.
    }

    private void btn_a_Click(object sender, RoutedEventArgs e)
    {
        button_full_details = (Control)sender;
        all_in_one();

        //var caretIndex = txt_diplay_1.CaretIndex;
        //txt_diplay_1.Text = txt_diplay_1.Text.Insert(caretIndex, btn_a.Content.ToString());
        //txt_diplay_1.Focus(); 
        //txt_diplay_1.CaretIndex = caretIndex + 1;    


    }

     private void btn_b_Click(object sender, RoutedEventArgs e)
    {
        button_full_details = (Control)sender;

        all_in_one();
    }



    private void btn_c_Click(object sender, RoutedEventArgs e)
    {
        button_full_details = (Control)sender;

        all_in_one();
    }

    private void txt_diplay_1_GotFocus(object sender, RoutedEventArgs e)
    {
        texbox_full_details = (Control)sender;           

    }

    private void txt_diplay_2_GotFocus(object sender, RoutedEventArgs e)
    {
        texbox_full_details = (Control)sender;
    }


    public void all_in_one()
    {
        keyboard_button = button_full_details as Button;
        if (texbox_full_details != null)
        {
            //TextBox tb = texbox as TextBox;
            //tb.Text += btn.Content;

            TextBox txt_box = texbox_full_details as TextBox;
            var caret_index = txt_box.CaretIndex;
            txt_box.Text = txt_box.Text.Insert(caret_index, keyboard_button.Content.ToString());
            txt_box.Focus();
            txt_box.CaretIndex = caret_index + 1;               
        }           

    }
}

它的输出将是这样的

result1

但需要这样的输出

Result2

当点击按钮时,它的内容将绑定在textbox中。那时当前绑定的文本框文本的背景颜色,字体颜色和字体大小应该改变。我该怎么做才能获得那种输出。请帮助我。 / p>

2 个答案:

答案 0 :(得分:1)

查看SelectedText,SelectionStart和SelectionLength。 http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.aspx

tb.SelectionStart = tb.Length - 1;
tb.SelectionLength = 1;

答案 1 :(得分:1)

好的,所以你想要在给定的条件下,你的TextBox文本缩放 因此,首先创建一个具有两个属性的类:例如EditedText和IsZoomed:

public class ZoomableText
{
    public string EditedText { get; set; }
    public Boolean IsZoomed { get; set; }
}

然后使用Xaml:在IsZoomed上使用带有DataTrigger的样式,并在IsZoomed为true时更改所需的文本方面。您可以在窗口资源或应用程序资源中声明此样式。示例:

    <Style TargetType="TextBox" x:Key="LargerWhenFocusedTextBox">
        <Setter Property="FontSize" Value="12" />
        <Setter Property="FontWeight" Value="Normal" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsZoomed}" Value="True">
                <Setter Property="FontSize" Value="14" />
                <Setter Property="FontWeight" Value="Bold" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

要使用它,只需执行以下操作:

     <StackPanel >
        <TextBox  Text="{Binding EditedText}" 
                  Style="{StaticResource LargerWhenFocusedTextBox}"  />
        <ToggleButton IsChecked="{Binding IsZoomed}" Content="Zoomed?" />
      </StackPanel >

您将StackPanel的DataContext设置为ZoomableText对象的实例。

您可能希望让ZoomableText对象在其属性上实现INotifyPropertyChanged。

对于当前的更改,请处理ToggleButton的Checked事件。

请注意,如果您没有为该样式指定一个键,它将自动应用于您的所有TextBox。