WPF MVVM Focus Field on Load

If it makes you feel better (it makes me feel better) you can do this in Xaml using an attached property: http://msdn.microsoft.com/en-us/library/system.windows.input.focusmanager.focusedelement.aspx Anything you can do in codebehind you can do in Xaml if you know the tricks. Fortunately, you didn’t have to implement this trick – MS did it for you.

.NET TextBox – Handling the Enter Key

Add a keypress event and trap the enter key Programmatically it looks kinda like this: //add the handler to the textbox this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckEnterKeyPress); Then Add a handler in code… private void CheckEnterKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Return) { // Then Do your Thang } }

Disable button in WPF?

This should do it: <StackPanel> <TextBox x:Name=”TheTextBox” /> <Button Content=”Click Me”> <Button.Style> <Style TargetType=”Button”> <Setter Property=”IsEnabled” Value=”True” /> <Style.Triggers> <DataTrigger Binding=”{Binding Text, ElementName=TheTextBox}” Value=””> <Setter Property=”IsEnabled” Value=”False” /> </DataTrigger> </Style.Triggers> </Style> </Button.Style> </Button> </StackPanel>

WPF: simple TextBox data binding

Name2 is a field. WPF binds only to properties. Change it to: public string Name2 { get; set; } Be warned that with this minimal implementation, your TextBox won’t respond to programmatic changes to Name2. So for your timer update scenario, you’ll need to implement INotifyPropertyChanged: partial class Window1 : Window, INotifyPropertyChanged { public event … Read more

Adding new line of data to TextBox

If you use WinForms: Use the AppendText(myTxt) method on the TextBox instead (.net 3.5+): private void button1_Click(object sender, EventArgs e) { string sent = chatBox.Text; displayBox.AppendText(sent); displayBox.AppendText(Environment.NewLine); } Text in itself has typically a low memory footprint (you can say a lot within f.ex. 10kb which is “nothing”). The TextBox does not render all text … Read more