0

I am trying to locate a label by its tag so i can add an additional label beside when a certain condition is met.

in old school winform i would just use:

Label sublabel = Controls.Find(Sub.id, true).FirstOrDefault() as Label;

snippet of the view.xaml:

<Grid>
    <Label 
        Content = "{Binding Path = NodeName, Mode = OneWay}" 
        Background = "{Binding Path = NodeStatus, Mode = OneWay}"
        Tag="{Binding Path = Nodeid, Mode = OneWay}"

i have tried something along these lines:

var label = Grid.Children.OfType<Label>()
    .First(i => i.Tag == "tagid");

but generates

An object reference is required for the non-static field, method, or property 'Panel.Children'

Thanks for looking

3

1 Answer 1

5

You could give the Grid an x:Name in your XAML markup:

<Grid x:Name="theGrid">

...and refer to it by this name in your code:

var label = theGrid.Children.OfType<Label>() ...

Note that this is not MVVM by any means though. Using MVVM, you would access the Nodeid source property rather than the Tag property of the Label element.

0

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.