How to change visualstate from code behind? - visualstatemanager

How to change visualstate from code behind?
I tried the following. But itz not working. Help me please.
VisualStateManager.GoToState(Control, "Checked", true);

Try this,
VisualStateManager.GoToElementState(Control, "Checked", true);

Related

Highlight field content onFocus

Can anyone enlighten me to a way I can Highlight the content of an input field OnFocus preferably by XAML only?
So if a user bring focus to a field, it will highlight the string or whatever is in there so they can for example just tab to it, and replace the existing string as soon as they start typing instead of having to manually highlight and delete it first?
I've seen answers that require code, but wondering if there's a XAML only route? Thanks!
You can use AutoCompleteBox for this purpose and you won't have to write any code to acheieve this functionality. It already have this functionality and it will work as textbox too for you..
Let me know if you require any furter information.
Cheers!
Vinod
I highly doubt there would be XAML code that is equivalent to the TextBox.SelectAll() method.
It should be as easy as attaching each TextBox's GotKeyboardFocus event to a single event handler like this.
private void TextBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (sender is TextBox)
((TextBox)sender).SelectAll();
}
<TextBox GotKeyboardFocus="TextBox_GotKeyboardFocus" />

Programatically set the ToggleButton style within a treeviewitem

Does anyone know a way to set the style of a ToggleButton within a treeview item please?
As in something like treeviewItem.ToggleButton.Style = "Blah" if you see what I mean.
Many thanks.
should be like this.
mytogglebutton.Style = CType(TryFindResource("MyToggleButtonStyle"),Windows.Style)

WPF DataGrid has RowEditEnding but no RowEditEnded

I've bound an ObservableCollection to a DataGrid. When I change values in the DataGrid, the RowEditEnding event is raised. But the e.Row.Item is the object before editing, so you don't see the new values. I understand that because of the EditEnding. In Silverlight you have an EditEnded event, how can I get the object with the new values when I edit the DataGrid.
thanks,
Filip
From https://social.msdn.microsoft.com/Forums/en-US/c38fc695-d1ec-4252-87b7-feb484ee01e4/wpf-4-datagrid-roweditending, change the UpdateSourceTrigger of the Binding to PropertyChanged. The Property will then be updated immediately, before the RowEditEnding event, and the new value can be accessed from the RowEditEnding event handler.
For example, for a DataGridComboBoxColumn
SelectedItemBinding="{Binding ForTestResult, UpdateSourceTrigger=PropertyChanged}"
This seems a very simple way to solve this issue.
In addition, although I have not tried it, I think it should be easy to also access the original value before editing if your object implements IEditableObject.
Well, maybe this may help: http://wpf.codeplex.com/Thread/View.aspx?ThreadId=39356
http://blogs.msdn.com/b/vinsibal/archive/2009/04/14/5-more-random-gotchas-with-the-wpf-datagrid.aspx
Or this, see point number 5.
You'll have to tinker with it to get what you want I think, but I hope that helps! Or points you in a good direction.
This solution seems simple enough. Referred from msdn forum.
private void dgEmployees_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
Action action = delegate
{
Employee emp = e.Row.Item as Employee;
//do something nice to the employee
};
Dispatcher.BeginInvoke(action, System.Windows.Threading.DispatcherPriority.Background);
}
Attach to the ObservableCollection's changed event.
I bound to a DataTable and used the RowChanged event.
My fresh and IMHO fastest way is to add bool rowEdited=false, then set it to true inside DataGrid_RowEditEnding and put your code for 'editEnded' inside DataGrid_LayoutUpdated:
if (rowEdited)
{
//main code here//
rowEdited=false;
}
.

MouseLeftButtonDown is not getting fired

I have a WPF user control that is dervied from UserControl class. MouseLeftButtonDown is not getting fired at all for the contol. I added event handler and also tried as follows.
I guess it is handled somewhere else, how to debug and find where is it getting hanlded.. Any help is appreciated!
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
}
Did you have the Background of the UserControl set to something? If not, try setting it to
"Background=Transparent" and see if that works. If it doesn't work, can you post the XAML for your UserControl, as well as the XAML for its usage?

UIElement.AddHandler

Has this been removed from silverlight 3?
http://msdn.microsoft.com/en-us/library/ms598899%28VS.95%29.aspx
I see documentation on how to use it, but when I try it says UIElement doesn't have an addhandler method
Hm, seems to work for me..
UIElement asdf = new Canvas();
asdf.AddHandler(ucConveyor.MouseLeftButtonDownEvent, TestFunction, false);
Can you paste your code?

Resources