ScottS
ScottS

Reputation: 79

WPF Not sure how to change background of textbox in dynamically created stackpanel

In a WPF project, I have a stackpanel...

<StackPanel Name="spOptionsValue" Orientation="Vertical" Visibility="Collapsed" Margin="10 0 0 0"  KeyUp="SaveTestOptionsInXMLData2"></StackPanel>

That I dynamically load values into...

spOptionsValue.Children.Add(new TextBox { Text = attributeChild.Value, Height = 26, Name = "tbTestAttributeValue" });

If I find an error in the value of this textbox (after the user changes it), how can I make the background red and put the focus on this textbox?

When I try something like spOptionsValue.Children[loopIndex - 3] = Brushes.Red; I get a compiler error of "Cannot implicitly convert type System.Windows.Media.SolidColorBrush to System.Windows.UIElement. I've also tried to work background into this statement without luck. Any advice would be appreciated!

Upvotes: 1

Views: 36

Answers (1)

FrankM
FrankM

Reputation: 1107

Try

((Control)spOptionsValue.Children[loopIndex - 3]).Background = Brushes.Red

The cast is necessary since UIElement has no Background property.

As for the focus, you would use:

((Control)spOptionsValue.Children[loopIndex - 3]).Focus();

Upvotes: 1

Related Questions