Wednesday, February 16, 2011

Focus & WPF

Sometimes, I would like to take the focus off an element.

I don't necessarily want to put the focus on another element.

In an instance, where the element is within a "StackPanel", you can do:

// XAML
< stackpanel focusmanager.isfocusscope="True" name="MyStackPanel" > ...

// C#

FocusManager.SetFocusedElement(MyStackPanel, null);

This will not focus on any element, and as importantly, remove focus on any element that has focus in the StackPanel.

Thursday, February 3, 2011

C# MySQL store BLOB

I needed to store a BLOB into a MySQL Database.

INSERT INTO `mytable` (`mycolumn1`, `mycolumn2`)
VALUES ('myValue1', @objectData);



_conn = new MySqlConnection();
_conn.ConnectionString = "MyConnectionString";
                   
// You must open the connection before Prepare()
_conn.Open();
 
MySqlCommand nonQueryCommand = new MySqlCommand(query, _conn);
 
nonQueryCommand.Prepare();
// Add parameter. myObjectData is my BLOB from C#
nonQueryCommand.Parameters.AddWithValue("@objectData", myObjectData);
 
nonQueryCommand.ExecuteNonQuery();
_conn.Close();