1

I'm having problem with my reaching, what i want to do is i want to reach this rectangle that is located in my UserControl:

<Rectangle x:Name="StatusColor" Margin="0,0,0,0" Height="218" VerticalAlignment="Top">
     <Rectangle.Fill>
         <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
              <GradientStop Color="#00000000" Offset="0.901"/>
              <GradientStop Color="#CC17FF00" Offset="0"/>
         </LinearGradientBrush>
     </Rectangle.Fill>
</Rectangle>

and be able to call it in my Main.xaml.cs for example like this:

StatusColor.Fill = RedBrush;

Right now I just receiving an error that says:

The name 'StatusColor' does not exist in the current context

I have two Xaml files named Main.xaml and the UserControl that is named TemplateGrid.xaml both files with one code behind file (Main.xaml.cs and TemplateGrid.xaml.cs)

So just to simplify.... I want to reach a X:name inside TemplateGrid.xaml from Main.xaml.cs.

Thanks in advance!

EDIT:

This is where i declare UserControl inside Main.xaml:

<GridView x:Name="GridView1" ItemContainerStyle="{StaticResource testGrid}" ItemsSource="{x:Bind testValue}" Width="1740" Height="835" IsHitTestVisible="False" ScrollViewer.HorizontalScrollBarVisibility="Hidden" Margin="75,190,75,100" FontFamily="Segoe MDL2 Assets" IsDoubleTapEnabled="False" IsHoldingEnabled="False" IsRightTapEnabled="False" IsTapEnabled="False">
    <GridView.ItemTemplate>
       <DataTemplate x:DataType="data:Room">
           <local:TemplateGrid x:Name="TemplateGridControl"/>
       </DataTemplate>
     </GridView.ItemTemplate>
</GridView>
luddep
  • 219
  • 1
  • 2
  • 14
  • 1
    https://stackoverflow.com/questions/11425153/how-to-make-a-control-in-xaml-public-in-order-to-be-seen-in-other-classes – Biesi May 30 '18 at 06:54

2 Answers2

1

In your TemplateGrid.xaml.cs create a new Property as below.

public Rectangle StatusColorProperty => StatusColor;

When you add your Usercontrol in Main.xaml set the property Name(x:Name ="TemplateGridControl") for the added usercontrol(TemplateGrid).

In Main.xaml.cs wherever you want access the StatusColor just do the following:-

TemplateGrid control = TemplateGridControl;
control.StatusColorProperty.Fill = Brushes.Red;

By default the controls inside the Usercontrol will be "Private" , so you need to create a public property which will exposes the control to outer environments.

Akhitha M D
  • 164
  • 4
  • Okey, now there is one thing i dont understand in your anwer, and that is when you say that i should set the property name in `Main.xaml`, where should i do that, the rectangle `StatusColor` is located in TemplateGrid – luddep May 30 '18 at 07:23
  • When you add the usercontrol in your Main.xaml, in XAML code you can set the property for usercontrol. `` – Akhitha M D May 30 '18 at 07:42
  • Check the edit. Is this what you mean? Cause I could not reach `TemplateGridControl` in my `Main.xaml.cs`. – luddep May 30 '18 at 07:49
  • I checked your new edited version and that's what I was talking about. You have already done that part.So you only have to add a public property in `TemplateGrid.xaml.cs` and add the code in `Main.xaml.cs` as mentioned in answer. Please ignore the `Main.xaml` part – Akhitha M D May 30 '18 at 09:29
  • Okey, but the part where you set `StatusColorProperty.Fill = Brushes.Red` I cant reach Brushes... is it any using? – luddep May 30 '18 at 09:53
  • `using System.Windows.Media;` – Akhitha M D May 30 '18 at 10:07
  • Okey, Media does not work, and the name TemplateGridControl isn't reachable inside `Main.xaml.cs`. – luddep May 30 '18 at 11:45
1

Use this VisualTreeHelper to find your Control:

private DependencyObject FindChildControl<T>(DependencyObject control, string ctrlName)
{
    int childNumber = VisualTreeHelper.GetChildrenCount(control);
    for (int i = 0; i < childNumber; i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(control, i);
        FrameworkElement fe = child as FrameworkElement;
        // Not a framework element or is null
        if (fe == null) return null;

        if (child is T && fe.Name == ctrlName)
        {
            // Found the control so return
            return child;
        }
        else
        {
            // Not found it - search children
            DependencyObject nextLevel = FindChildControl<T>(child, ctrlName);
            if (nextLevel != null)
                return nextLevel;
        }
    }
    return null;
}

Because the UserControl was contained in the DataTemplate So, you could only get a Rectangle instance from selected GridView item at a time.

 Rectangle Rec = FindChildControl<Rectangle>(GridView1.SelectedItem, "StatusColor") as Rectangle;
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
Hubii
  • 348
  • 1
  • 14
  • When I try your solution i get this error **System.NullReferenceException: 'Object reference not set to an instance of an object.'** – luddep May 30 '18 at 11:19
  • I'm not really sure what you mean with your anwer... But thanks a lot for trying to help :) – luddep May 30 '18 at 12:33