|
Query by Mr. Rajpreet Singh
I have a query in my WPF
application.
I was trying to generate
XAML styles at runtime using
the following code:
XNamespace
xmlns =
XNamespace.Get("http://schemas.microsoft.com/winfx/2006/xaml/presentation");
XNamespace
x =
XNamespace.Get("http://schemas.microsoft.com/winfx/2006/xaml");
XDocument
xamlDoc =
new
XDocument(
new
XElement(xmlns +
"ResourceDictionary",
new
XAttribute(XNamespace.Xmlns
+
"x",
x.NamespaceName)));
xamlDoc.Root.Add(
new
XElement("SolidColorBrush",
new
XAttribute(x +
"Key",
"ButtonBrush"),
new
XAttribute("Color",
"Red")));
The output which I get is as
shown below, but it also
brings this (highlighted)
useless attribute.
<ResourceDictionary
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<SolidColorBrush
x:Key="AutoBrush"
Color="Black"
xmlns=""
/>
</ResourceDictionary>
Please tell me what I should
change in the code to get
rid of this. For now I have
manually removed it from the
XAML string.
I have also tried combing
both the statements into a
single one but that also
doesn’t help:
XDocument
xamlDoc =
new
XDocument(
new
XElement(xmlns +
"ResourceDictionary",
new
XAttribute(XNamespace.Xmlns
+
"x",
x.NamespaceName),
new
XElement("SolidColorBrush",
new
XAttribute(x +
"Key",
"ButtonBrush"),
new
XAttribute("Color",
"Red"))));
Solutions by Mario Fernandes
(Just add the variable I
have marked in green.)
XNamespace
xmlns =
XNamespace.Get("http://schemas.microsoft.com/winfx/2006/xaml/presentation");
XNamespace
x =
XNamespace.Get("http://schemas.microsoft.com/winfx/2006/xaml");
XDocument
xamlDoc =
new
XDocument(
new
XElement(xmlns +
"ResourceDictionary",
new
XAttribute(XNamespace.Xmlns
+
"x",
x.NamespaceName)));
xamlDoc.Root.Add(
new
XElement(xmlns
+
"SolidColorBrush",
new
XAttribute(x +
"Key",
"ButtonBrush"),
new
XAttribute("Color",
"Red")));
|