none
如何用textbox 编写一个类似控制台的程序 RRS feed

  • 问题

  • 退格键 可以删除以前的数据
    我像每次 都以sql> 开头

    用textbox.text+="/r/n+sql>"光标会改变

    还有 我把enter 事件 处理成执行了一个函数

    然后 我按回车只执行函数不换行了

    2011年5月3日 14:18

答案

  • 做个假的就可以,看下我的例子:

     public partial class MainWindow : Window
     {
      ConsoleHelper consolehelper = new ConsoleHelper();
      public MainWindow()
      {
       InitializeComponent();
       this.DataContext = consolehelper;
      }
    
      private void Input_KeyDown(object sender, KeyEventArgs e)
      {
       if (e.Key == Key.Return)
       {
        // TO DO: ...
        consolehelper.Output.Add("sql>" + consolehelper.Input);
        consolehelper.Input = string.Empty;
       }
      }
     }
    
     public class ConsoleHelper : INotifyPropertyChanged
     {
      public ConsoleHelper()
      {
       Output = new ObservableCollection<string>();
      }
    
      string input;
      public string Input
      {
       get { return input; }
       set
       {
        input = value;
        OnPropertyChanged("Input");
       }
      }
    
      ObservableCollection<string> output;
      public ObservableCollection<string> Output
      {
       get { return output; }
       set
       {
        output = value;
        OnPropertyChanged("Output");
       }
      }
    
      private void OnPropertyChanged(string p)
      {
       if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(p));
      }
    
      public event PropertyChangedEventHandler PropertyChanged;
     }
    

     

    XAML:

    <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="350" Width="525">
     <Grid>
      <Grid.RowDefinitions>
       <RowDefinition Height="*"/>
       <RowDefinition Height="Auto"/>
      </Grid.RowDefinitions>
      <ListBox Background="Black" ItemsSource="{Binding Output}">
       <ListBox.ItemTemplate>
        <DataTemplate>
         <TextBlock Text="{Binding}" Foreground="LightGreen"/>
        </DataTemplate>
       </ListBox.ItemTemplate>
      </ListBox>
      <DockPanel Grid.Row="1" LastChildFill="True">
       <TextBlock DockPanel.Dock="Left" Text="sql>" Background="Black" Foreground="LightGreen"/>
       <TextBox x:Name="Input" BorderBrush="Transparent" BorderThickness="0" Grid.Row="1" Background="Black" Foreground="LightGreen" Text="{Binding Input, UpdateSourceTrigger=PropertyChanged}" KeyDown="Input_KeyDown"/>
      </DockPanel>
     </Grid>
    </Window>
    
    

     

    J


    Best day, Best life
    • 已标记为答案 MercyDLark 2011年5月3日 14:35
    2011年5月3日 14:34

全部回复

  • 做个假的就可以,看下我的例子:

     public partial class MainWindow : Window
     {
      ConsoleHelper consolehelper = new ConsoleHelper();
      public MainWindow()
      {
       InitializeComponent();
       this.DataContext = consolehelper;
      }
    
      private void Input_KeyDown(object sender, KeyEventArgs e)
      {
       if (e.Key == Key.Return)
       {
        // TO DO: ...
        consolehelper.Output.Add("sql>" + consolehelper.Input);
        consolehelper.Input = string.Empty;
       }
      }
     }
    
     public class ConsoleHelper : INotifyPropertyChanged
     {
      public ConsoleHelper()
      {
       Output = new ObservableCollection<string>();
      }
    
      string input;
      public string Input
      {
       get { return input; }
       set
       {
        input = value;
        OnPropertyChanged("Input");
       }
      }
    
      ObservableCollection<string> output;
      public ObservableCollection<string> Output
      {
       get { return output; }
       set
       {
        output = value;
        OnPropertyChanged("Output");
       }
      }
    
      private void OnPropertyChanged(string p)
      {
       if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(p));
      }
    
      public event PropertyChangedEventHandler PropertyChanged;
     }
    

     

    XAML:

    <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="350" Width="525">
     <Grid>
      <Grid.RowDefinitions>
       <RowDefinition Height="*"/>
       <RowDefinition Height="Auto"/>
      </Grid.RowDefinitions>
      <ListBox Background="Black" ItemsSource="{Binding Output}">
       <ListBox.ItemTemplate>
        <DataTemplate>
         <TextBlock Text="{Binding}" Foreground="LightGreen"/>
        </DataTemplate>
       </ListBox.ItemTemplate>
      </ListBox>
      <DockPanel Grid.Row="1" LastChildFill="True">
       <TextBlock DockPanel.Dock="Left" Text="sql>" Background="Black" Foreground="LightGreen"/>
       <TextBox x:Name="Input" BorderBrush="Transparent" BorderThickness="0" Grid.Row="1" Background="Black" Foreground="LightGreen" Text="{Binding Input, UpdateSourceTrigger=PropertyChanged}" KeyDown="Input_KeyDown"/>
      </DockPanel>
     </Grid>
    </Window>
    
    

     

    J


    Best day, Best life
    • 已标记为答案 MercyDLark 2011年5月3日 14:35
    2011年5月3日 14:34
  • 关于回车换行问题.. TextBoxBase有个AcceptsReturn属性可以参考下..
    just another day.
    2011年5月3日 14:46