1
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;               
        }           

    }
}

它的输出将是这样的

结果1

但需要这样的输出

结果2

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

4

2 回答 2

1

Look into SelectedText, SelectionStart and SelectionLength. http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.aspx

tb.SelectionStart = tb.Length - 1;
tb.SelectionLength = 1;
于 2012-06-13T10:05:20.097 回答
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 事件。

请注意,如果您没有为样式提供 Key,它将自动应用于您的所有 TextBox。

于 2012-06-13T10:23:17.770 回答