Hell all, I am currently trying to create a few object dynamically in code. This has been successful up to the point to where I want to identify the element for example
Here I create the object ( an ellipse with a drop shadow)
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var element = CreateEllipse();
LayoutRoot.Children.Add(element);
}
public Ellipse CreateEllipse()
{
StringBuilder xaml = new StringBuilder();
string ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
xaml.Append("<Ellipse ");
xaml.Append(string.Format("xmlns='{0}'", ns));
xaml.Append(" x:Name='myellipse'"); //causes the exception
xaml.Append(" Margin='50 10 50 10'");
xaml.Append(" Grid.Row='0'");
xaml.Append(" Fill='#FD2424FF'");
xaml.Append(" Stroke='Black' >");
xaml.Append("<Ellipse.Effect>");
xaml.Append("<DropShadowEffect/>");
xaml.Append(" </Ellipse.Effect>");
xaml.Append(" </Ellipse>");
var ellipse = (Ellipse)XamlReader.Load(xaml.ToString());
return ellipse;
}
What I want to do is after the object is created I want to be able to locate the parent objects using VisualTreeHelper.
public void button1_Click(object sender, RoutedEventArgs e)
{
DependencyObject o = myellipse;
while ((o = VisualTreeHelper.GetParent(o)) != null)
{
textBox1.Text = (o.GetType().ToString());
}
}
Can anyone point me in the right direction for referencing a dynamically created object in a scenario like this or how to properly define x:Name for an object programatically?
Thank you,
You can’t reference “myellipse” directly like this with dynamically generated types. The compiler converts the
x:Nameto a type at compile time – since you’re loading this at runtime, it will not exist.You could, instead, make a “myellipse” variable, and have your dynamic generation routine set it:
That being said, I would recommend not building the ellipse using strings like this. You could just create the ellipse class directly, and add the effect. You don’t need to use
XamlReaderto parse a string in this case.