2012-06-13 59 views
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;    
     }   

    } 
} 

它的輸出會是這樣如何突出文本框在WPF的當前輸入

result1

不過需要這樣

Result2

輸出當單擊該按鈕,它的內容將綁定到文本框中。那時,當前綁定的文本框文本的背景色,字體顏色和字體大小應該會改變。我該怎麼做才能得到那種輸出。請幫助我。

回答

1

好了,所以你想要的,在給定的條件下,您的文本框的文字放大。
所以首先創建一個類具有兩個屬性:EditedText和IsZoomed例如:

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

然後使用XAML中:使用樣式與IsZoomed一個DataTrigger,並改變你希望在IsZoomed是真正的文本方面。您可以在窗口資源或應用程序資源中聲明此樣式。例如:

<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。

對於curret更改,請處理ToggleButton的Checked事件。

請注意,如果您不給樣式的鍵,它會自動應用於所有文本框。

+0

這是不work.Please嘗試與我的代碼。我的編輯 –

+0

現在應該可以。 – GameAlchemist

+0

對不起文森特皮爾,我無法得到我需要的結果。我正在創建一個屏幕上的鍵盤。這就是我需要動態創建它。實際上,我需要當前鍵入(按鈕內容如a, b顯示在上圖)文本應該突出顯示給用戶。請幫助我... –

相關問題