Saturday, January 28, 2012

XAML: Changing a Style on MouseOver of the Parent Item

Sometimes, you want a Mouse Over event to trigger a style change on some or all the items within a container.

Changing Image style when a Mouse moves over a custom parent container (WC_MenuItem). Any type of container or Panel can be substituted for the custom type I've used.

<Image x:Name="imgHelpLink" Source="pack://application:,,,/Weblink_WPF;component/Images/Help.png" Cursor="Hand" Grid.Column="1" Width="16" Height="16" Margin="0,0,0,0" ToolTip="Click to view the Knowledgebase article on this report" HorizontalAlignment="Right" >
<Style TargetType="{x:Type Image}">
<Setter Property="Opacity" Value="0" />
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type weblink_controls:WC_MenuItem}},Path=IsMouseOver}" Value="True">
<Setter Property="Opacity" Value="1" />
<!--<DataTrigger Binding="{Binding Path=HelpLink.Length}" Value="0">
<Setter Property="Opacity" Value="0.3"/>

Serializing and DeSerializing Objects as XML in VB.NET

It was unbelievably difficult to find a working example where the XML could come from a web server on a different domain. So, I guess I will share with the world how I managed to make it work.

Serialize List of Objects to XML
(Web Server)

Dim xml As String = ""
Dim mstr_XmlFilePath As String = "/myFolder/myFile.xml"
Dim mstr_ServerXmlFilePath As String = Server.mapPath(mstr_XmlFilePath)
Dim obj_objMyObject As New objMyObject
Dim lst_objMyObject As List(Of objMyObject) = [[***populate object***]]


Dim ser As XmlSerializer
ser = New XmlSerializer(GetType(System.Collections.Generic.List(Of objMyObject)))
Dim memStream As New MemoryStream
Dim xmlWriter As New XmlTextWriter(memStream, Encoding.UTF8)
xmlWriter.Namespaces = True
ser.Serialize(xmlWriter, lst_objMyObject)

xml = Encoding.UTF8.GetString(memStream.GetBuffer())
xml = xml.Substring(xml.IndexOf(Convert.ToChar(60)))
xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1))

My.Computer.FileSystem.WriteAllText(XmlFilePath, xml, False)

Catch ex As Exception
Throw New Exception("Error calling ToXML. " & ex.GetBaseException.ToString)
End Try

DeSerialize List of Objects from XML (Client or Cross-Domain Web Server)

str_XmlFullFilePath = "myURL" + mstr_XmlFilePath
Dim objListFromXml As New List(Of objMyObject)

Dim reader As System.Xml.XmlReader
reader = System.Xml.XmlReader.Create(str_XmlFullFilePath)

Dim mySerializer As XmlSerializer = New XmlSerializer(GetType(System.Collections.Generic.List(Of objMyObject)))
objListFromXml = CType(mySerializer.Deserialize(reader), System.Collections.Generic.List(Of objMyObject))

'don't do anything. If error occurs, return an empty object
End Try

I may have some typos above. I replaced all my variables with more generic ones.