はい、できます。イベントを処理する必要があります。たとえば、この場合は 'TextBox.GotFocus`イベントを処理します。
void tb_GotFocus(object sender, GotFocusEventArgs e)
{
// here you can get the StackPanel as the parent of the textBox and
// search for the Lable
TextBox tb=(TextBox)sender;
StackPanel sp=tb.Parent as StackPanel;
// and ...
}
この例を完成させたい場合は、私に知らせてください。
編集
これは実際の例です:
このウィンドウを使用して結果を表示します。
Window win = new Window();
StackPanel stack = new StackPanel { Orientation = Orientation.Vertical };
stack.Children.Add(new CustomControl());
stack.Children.Add(new CustomControl());
stack.Children.Add(new CustomControl());
stack.Children.Add(new CustomControl());
win.Content = stack;
win.ShowDialog();
CustomControl クラスは次のとおりです。
public class CustomControl : Border
{
Label theLabel = new Label {Content="LableText" };
TextBox theTextbox = new TextBox {MinWidth=100 };
public CustomControl()
{
StackPanel sp = new StackPanel { Orientation=Orientation.Horizontal};
this.Child = sp;
sp.Children.Add(theLabel);
sp.Children.Add(theTextbox);
theTextbox.GotFocus += new RoutedEventHandler(tb_GotFocus);
theTextbox.LostFocus += new RoutedEventHandler(tb_LostFocus);
}
void tb_GotFocus(object sender, RoutedEventArgs e)
{
theLabel.Background = Brushes.Yellow;
}
void tb_LostFocus(object sender, RoutedEventArgs e)
{
theLabel.Background = Brushes.Transparent;
}
}