doull
doull

Reputation: 121

wpf,How to find mainControl?

How to find UserControl Layer by grid?

<UserControl
 ... ...
 >
  <Grid name="grid">
  </Grid>

I want to add resource to UserControl Layer in CodeBehind.

Grid contain lots of TextBox. it is invalid when i add resource to Grid.

ResourceDictionary resource = new ResourceDictionary();
            Style style = new Style(typeof(TextBox));
            style.Setters.Add(new Setter(TextBox.BorderThicknessProperty, new Thickness(0))); 

Upvotes: 0

Views: 77

Answers (2)

decyclone
decyclone

Reputation: 30840

If you want a reference to UserControl in code behind, you can use this keyword.

If you want to refer to it in a binding, use Ancestor binding like following :

{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=PropertyName}

Upvotes: 1

bitbonk
bitbonk

Reputation: 49659

You can use the VisualTreeHelper to navigate and search in the visual tree.

Upvotes: 1

Related Questions