Silverlight MVVM - Twoway binding not fired on Datepicker - c#

I have Silverlight MVVMLight 4.0 application in which I have datepicker.
The datepicker is two way bound to the viewmodel. There is no code behind.
This works fine when tabbing of the datepickers textbox and will change the underlying property of the object.
But when I change the text box and don't tab off and click save the change is not registered.
I have looked at the various events that are fired and they don't fire unless you tab off
private void startDateDatePicker_TextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
}
private void startDateDatePicker_TextInputStart(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
}
private void startDateDatePicker_TextInputUpdate(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
}
private void startDateDatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
}
Does anybody have a workaround for this?
kind regards,
Pat

I'm fairly sure that the TextBox needs to lose focus before the property is updated.
You could try handling the KeyDown event of the TextBox and update the property from there, but I'm not sure how well that would behave.

Change the UpdateSourceTrigger value in your binding.
If is not set, uses the default (which is the Lost Focus for TextBox.Text)
Depending the inner-workings and needs of your screen, you can set it to PropertyChanged or Explicit.
http://msdn.microsoft.com/en-us/library/ms752347.aspx

Related

Prevent ComboBox dropdown and its AutoComplete dropdown appearance conflict

I have a ComboBox whose DropDownStyle is DropDown, allowing the user to type into it and its AutoCompleteMode is Suggest. The problem is that if the ComboBox is currently open and the user starts typing into it the auxiliary drop-down list appears but clicking on an item from it actually selects the item from the ComboBox's original drop-down list residing under the mouse at the time of click.
I would prefer if while the ComboBox's drop-down list is open the user could not type into it and would like to know if there is a more elegant solution than:
Setting the AutoCompleteMode to None when the ComboBox is open
Possibly changing the DropDownStyle to DropDownList on the OnClick event (haven't tried, but the theory is sound)
Manipulating (or restricting) the entered text while the list is open
Similar
As an option you can handle KeyPress event of the ComboBox and close the dropdown. It keeps the autocomplete menu open, but closes dropdown:
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
this.comboBox1.DroppedDown = false;
}
As another option, you can handle DropDown and DropDownClosed events of the ComboBox and disable autocomplete in DropDown and enable it again in DropDownClosed event:
private void comboBox1_DropDown(object sender, EventArgs e)
{
this.comboBox1.AutoCompleteMode = AutoCompleteMode.None;
}
private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
this.comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
}
You can create a new class deriving from ComboBox and override corresponding OnXXXX methods and put the logic there. This way you encapsulate the fix in the control class instead of handling events in your form and will have a reusable bug-free control and a more clean code.

SelectOnEntry for wpf textbox?

I want to select all text into a textbox just when it got focus , there is a property named SelectOnEntry in wpf texbox or an equivalent?
If Not how to implment it ?
Attach handler for GotFocus event:
<TextBox GotFocus="TextBox_GotFocus"/>
and in handler:
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
((TextBox)sender).SelectAll();
}
If you need to do it in pure XAML way, you can create an attached property to select all text on focus. Refer to the solution here.

How to check if DataGrid control has lost focus

After datagrid gets focused it shows some buttons on the form. But what is the event that I should use to hide those buttons after datagrid loses it's focus? I should probably mention that this project is done in .Net 2.0.
You should register on dataGridView.Leave event.
gridview.LostFocus += new EventHandler(gridview_LostFocus);
public void gridview_LostFocus(object sender, EventArgs e)
{
//Do stuff when focus is lost
}

C# autocomplete combobox trigger SelectionChangeCommited

I'm having problems with the autocomplete property of a combobox. I want to trigger the SelectionChangeCommited event every time I choose an item using the autocomplete but it's not working. The only way the event is triggered is when I use the mouse click and select an option or when the combobox is focused and I use arrow keys on the keyboard. How do I achieve this behaviour using the autocomplete property?
My combo has these properties set:
AutoCompleteMode = SuggestAppend
AutoCompleteSource = ListItems
FormattingEnabled = True
The items in my combo are set with a datasource.
Any ideas?
Thanks
If you mean that you want it to register a change when you start typing:
Call the SelectionChangeCommited event from the TextChanged event.
If you've never done this, the most basic example I could find was on the .net forums here. Granted, the methods shown there are generalities, but is very simple to understand and apply to your code.
EDIT FIXED (as of most recent comment):
Still tie the events together, but instead of using TextChanged, which would occur ever time you type, use the SelectedIndexChanged, which occurs when you use the mouse to select an auto suggested item.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1_SelectionChangeCommitted(sender, e);
}
you canuse a trick and call comboBox1_SelectionChangeCommitted
in Validated event
when ever text in combobox changes and user leave the combo box it will be fired
private void comboBox1_Validated(object sender, EventArgs e)
{
comboBox1_SelectionChangeCommitted(sender, e);
}

C# combobox selection feeding another combobox

using mysql I feed combobox2 with selection of combobox1.It works OK. but the problem is the first select seems does not trigger the eventhandler. the second time I do it it will trigger.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.SelectedValueChanged += new EventHandler(comboBox1_selectedvaluechanged);
}
private void comboBox1_selectedvaluechanged(object sender, EventArgs e)
{
region = comboBox1.SelectedItem.ToString();
values_to_venue();
db.connection.Close();
}
It's because you're not creating your event handler until the SelectedIndex of the combobox changes. This is fired alongside SelectedValue changes. Create the event handler in the load method ensuring it is there when the first SelectedValue changes. If you put it in the Load make sure you clean it up with -= in the unload. Or, you can just create it in the constructor and then you won't need to remove it.
You do want to set the events in your constructor or load method; also, I think the event you want to use is ComboBox.SelectionChangeCommitted because if the user navigates the DropDown list using the up/down arrows on the keypad, SelectedIndexChanged will fire before the selection is committed - is that what you want??????

Categories

Resources