Gosh, I'm currently trying to create multiple objects dynamically in code. This was successful until the moment when I want to identify the element, for example, Here I create an object (ellipse with 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'");
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 creating the object. I want to be able to find 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 someone point me in the right direction to refer to a dynamically created object in such a scenario or how to correctly determine x: Name for the object programmatically?
Thank,
source
share