积极答复者
如何用textbox 编写一个类似控制台的程序

问题
答案
-
做个假的就可以,看下我的例子:
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
全部回复
-
做个假的就可以,看下我的例子:
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