0

How can I edit/modify child elements, that have already been added?

I have a grid with added child element border that is set to specific color. How can I change the existing border color without adding a new child element?

Because I need these elements to be created dynamically, I've used code instead of xaml for that.
I define my grid and add a border to it as follows:

    Dim NewNodeGrid As New Grid
    Dim Bcgkgr As New SolidColorBrush
    Dim MyBorder As New Border

    Bcgkgr.Color = Color.FromArgb(255, 85, 57, 33)

    MyBorder.BorderBrush = Brushes.Black
    MyBorder.BorderThickness = New Thickness(5, 5, 5, 5)
    MyBorder.Background = Bcgkgr
    MyBorder.CornerRadius = New CornerRadius(15)
    MyBorder.Name = "Border"

    NewNodeGrid.Children.Add(MyBorder)

So after this is done, is it possible to access this child element MyBorder and change its properties like BorderBrush ?

I've tried something like: NewNodeGrid.Children.Item(0).BorderBrush = Brushes.Blue, but that doesn't work.

Edit:

The code above would be run multiple times, thus creating multiple instances of the NewNodegrid. I need to be able to change the border of any given single/multiple instances, not all at once.

Edit2:

An example to illustrate what I'm trying to achieve:

I want to create multiple objects (new instances of a defined object) at runtime. And after that, I want change some property of any of those objects.

It would look like this:
example
By using code above (similar version), I create two objects with borders. After that I want to change the border color of one object, when I click on the said object.

4
  • did you try to set MyBorder.BorderBrush from Code ?
    – stsur
    Jan 27, 2015 at 8:58
  • Yes, that works for one element, but I need to be able to set different brushes for multiple/different instances of the NewNodeGrid
    – Emi
    Jan 27, 2015 at 9:09
  • You could simply cast the child elements to type Border (provided that they actually are Borders). If you have to set the BorderBrush of multiple Borders to the same color, it might be easier to assign a single SolidColorBrush to all Borders and then just change the Color property of this SolidColorBrush.
    – Clemens
    Jan 27, 2015 at 9:10
  • I don't think that will work for me. The code above would be run multiple times, thus creating multiple instances of the NewNodegrid. I need to be able to change the border of any given single/multiple instances, not all at once. Sorry for vagueness. Edited the post.
    – Emi
    Jan 27, 2015 at 9:22

2 Answers 2

0

Just because you're not using XAML does not mean that you cannot use data binding. You may want to refer to the How to: Create a Binding in Code page on MSDN for further help, but in short, you can programmatically setup a binding like this example (taken from the linked page):

MyData myDataObject = new MyData(DateTime.Now);      
Binding myBinding = new Binding("MyDataProperty");
myBinding.Source = myDataObject;
myText.SetBinding(TextBlock.TextProperty, myBinding);

With a data binding setup, all you need to do is to change the data bound property value to update the value in the UI... continuing with this example:

myDataObject.MyDataProperty = "Some new value";
5
  • Not sure how to implement this for multiple instance objects. Should I create a public property for the said object and try to bind that to some internal control (BorderBrush)?
    – Emi
    Jan 28, 2015 at 8:31
  • What do you mean by multi instance objects?
    – Sheridan
    Jan 28, 2015 at 9:01
  • I would need to able to create multiple objects (new instances of a defined object) at runtime. And after that, I would need to be able to change some property of any of those objects. For example: i.imgur.com/j4I71sP.png By using code above (similar version), I create two objects with borders. After that I want to change the border color of one object, when I click on the said object.
    – Emi
    Jan 28, 2015 at 9:15
  • Added further edits to original post to illustrate what I want to achieve.
    – Emi
    Jan 28, 2015 at 9:25
  • It sounds like you need to define a DataTemplate. See the Data Templating Overview page on MSDN for further help.
    – Sheridan
    Jan 28, 2015 at 10:53
0

After endless series of trial and error, and persistent searches, I've found a solution that worked for me.

To change the border color of any added Grid element with a mouse click on it, the mouse_down event looks like this:

Dim ExistingBorder = CType(sender, Grid).Children.OfType(Of Border)().FirstOrDefault()
ExistingBorder.BorderBrush = Brushes.Red

Got this idea from this post: how to get the children of an element WPF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.