C#可以针对像jQuery这样的选择器吗?

C#可以针对像jQuery这样的选择器吗?,c#,jquery,wpf,events,layout,C#,Jquery,Wpf,Events,Layout,jQuery可以选择某物的子项、父项或下一个标记。C#是否有类似的行为。例如,我有一个水平的堆栈面板,它有一个标签、一个文本框和一个按钮 如果当前选择了文本框,而未使用引用标签名称的if-else语句,我是否可以将标签作为更改颜色的目标?此文本框所在的堆栈面板中的某些功能类似于“此标签” 示例:无论何时选择文本框,其旁边的标签都会变为黄色背景 我不知道如何用C#写这篇文章,所以我将尝试用常规文本进行解释 if (this) textbox is selected get the lab

jQuery可以选择某物的子项、父项或下一个标记。C#是否有类似的行为。例如,我有一个水平的
堆栈面板
,它有一个
标签
、一个文本框和一个
按钮

如果当前选择了
文本框
,而未使用引用
标签
名称的if-else语句,我是否可以将标签作为更改颜色的目标?此
文本框
所在的
堆栈面板
中的某些功能类似于“此标签”

示例:无论何时选择文本框,其旁边的标签都会变为黄色背景

我不知道如何用C#写这篇文章,所以我将尝试用常规文本进行解释

if (this) textbox is selected
    get the label next to it
    change the label background to a color

else
    remove the color if it is not selected
方法()将根据当前选择的
文本框
触发


这样的函数可以具有相同的代码,但一旦焦点更改为不同的
文本框
,就可以针对不同的
标签
。这可能吗?

是的,你可以;你应该处理事件。例如,在本例中,处理“TextBox.GotFocus”

如果你想完成这个例子,让我知道

编辑
这是一个工作示例:

使用此窗口显示结果:

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;
    }
}

每个控件都有
父控件
子控件
,但是没有特定的CSS选择器功能,除非您自己构建它。您尝试了什么?你能更清楚一点你想做什么吗?你的问题不是很清楚?你肯定不是说XAML?是的,我指的是C#behavior。C#是一种编程语言,与JavaScript类似。它没有能力“选择”任何东西。相反,它让你有能力写这样的东西。我试过这样的东西。但一旦我有了20个文本框,我就必须将事件添加到所有20个文本框中。可以创建一个方法来更改所选
文本框旁边的
标签
颜色吗?然后,我认为最好的方法是创建一个自定义控件。我将编辑我的答案。@MiddleCSharp查看编辑。@MiddleCSharp我编辑了我的帖子。我以为你只是想要这个主意。对不起,谢谢你,拉明。我已经稍微重新格式化了你的代码。但我收到了3个方面的错误,所以我无法验证这是否有效。theTextbox.GotFocus=tb\u GF;而GotFocusEventArgs不为我工作
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;
    }
}