2012-05-03 30 views
2

私は文字列ビルダに入力されたデータを追加するために私のウィンドウでkey_upイベントを使用しています。wpfウィンドウからテキスト入力を取得

<Grid x:Name="grdMain" KeyUp="grdMain_KeyUp"> 
     <Grid.RowDefinitions>... 

    private StringBuilder buffer = new StringBuilder(); 
    private void Window_KeyUp(object sender, KeyEventArgs e) 
    { 
     if (e.Key != Key.Enter) 
     { 
      buffer.Append(e.Key); 
     } 

事がバッファに印加されたデータは、 は、私が何かが欠けています...代わりに、代わりに「3」の「4」と「D3」の「NumPad4」であるということですか?それがテキストボックスに入力されたかのようにデータを追加する方法はありますか? 私は自分のデータを変換することができることを知っていますが、これを行う方法が組み込まれていないのは奇妙に思えます。

+0

レイアウトコンテナにはTextChangedイベントがありません。テキストはありません。奇妙なことがあなたがやっているものです。テキストを受け取るためにグリッドが必要なのはなぜですか? – EvAlex

+0

実際には顧客のリクエストです。彼は焦点がどこにあるのか心配したくない。 – Yoav

答えて

0

これは一見一見不思議に思えるかもしれませんが、それは正しい方法だと思います。私はTextBoxから派生し、そのコンテンツをホストするためにContentPresenterとそのコンテンツを設定するためにContentプロパティを追加しました。

したがって、TextBoxContainerという名前のWPFカスタムコントロール(Visual Studioテンプレート)を作成します。それに次のプロパティを追加しますことを

<local:TextBoxContainer TextChanged="TextBoxGrid_TextChanged" > 
    <local:TextBoxContainer.Content> 
     <Grid> 
      <!-- Any markup here --> 
     </Grid> 
    </local:TextBoxContainer.Content> 
</local:TextBoxContainer> 

注:

<LinearGradientBrush x:Key="TextBoxBorder" EndPoint="0,20" MappingMode="Absolute" StartPoint="0,0"> 
    <GradientStop Color="#ABADB3" Offset="0.05"/> 
    <GradientStop Color="#E2E3EA" Offset="0.07"/> 
    <GradientStop Color="#E3E9EF" Offset="1"/> 
</LinearGradientBrush> 
<Style TargetType="{x:Type local:TextBoxContainer}"> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> 
    <Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}"/> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="Padding" Value="1"/> 
    <Setter Property="AllowDrop" Value="true"/> 
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
    <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/> 
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:TextBoxContainer}"> 
       <Grid> 
        <Microsoft_Windows_Themes:ListBoxChrome x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" SnapsToDevicePixels="true"> 
         <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
        </Microsoft_Windows_Themes:ListBoxChrome> 
        <ContentPresenter Content="{TemplateBinding Content}"/> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsEnabled" Value="false"> 
         <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
         <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

そして、次のようにそれを使用します。

#region Content (DependencyProperty) 

    /// <summary> 
    /// Gets or sets Content property 
    /// </summary> 
    public object Content 
    { 
     get { return (object)GetValue(ContentProperty); } 
     set { SetValue(ContentProperty, value); } 
    } 
    public static readonly DependencyProperty ContentProperty = 
     DependencyProperty.Register("Content", typeof(object), typeof(TextBoxContainer), 
      new PropertyMetadata(null)); 

    #endregion 

は、それが次と資産/ generic.xamlファイルにスタイルだ置き換え入力されたすべてのテキストを含むテキストがいつでもあります。あなたが気づくべき唯一の事は、いくつかの要素がいくつかの出来事がバブリングするのを防ぐことです。たとえば、TextBoxは、イベントのバブリングを停止します(KeyDown)。あなたがTextBoxContainerの中にTextBoxを置き、ユーザーがそれをクリックしてそこにテキストを入力すると、TextBoxContainerはそのテキストを受信しませんが、TextChangedイベントが発生します。

関連する問題