sebamed
sebamed

Reputation: 1077

Access a border from XAML inC#

How can a border from XAML file be accessed in C# code? I searched but found nothing, kinda new to WPF, so don't know how to implement concepts.

My current border in XAML

I want to change the background color of this element in C#.

Thank you

EDIT

Here is the whole code

<Button x:Name="btnSNUcenik" Click="btnSNUcenik_Click">
            <Button.Template>
                <ControlTemplate TargetType="Button">
                    <DockPanel Width="215" Cursor="Hand">
                        <DockPanel.Style>
                            <Style TargetType="{x:Type DockPanel}">
                                <Setter Property="Background" Value="#2B303D"/>
                                <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="Background" Value="#242631"/>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </DockPanel.Style>

                        <!-- right here -->
                        <Border x:Name="brdrSNUcenik" Padding="10" HorizontalAlignment="Center" Width="148">
                            <Label Content="Ucenik" Foreground="Gray" FontSize="15" VerticalContentAlignment="Center" HorizontalContentAlignment="Right" VerticalAlignment="Center" Margin="19,0,0,0"/>
                        </Border>
                        <Border Padding="10" HorizontalAlignment="Right">
                            <Border Padding="9" Background="#242631">
                                <Image x:Name="icons8_Graduation_Cap_48px_png" Height="25" Source="Images/icons8_Graduation_Cap_48px.png" Stretch="Fill" Width="25"/>
                            </Border>
                        </Border>
                    </DockPanel>
                </ControlTemplate>
            </Button.Template>
        </Button>

Upvotes: 0

Views: 843

Answers (4)

sebamed
sebamed

Reputation: 1077

As @Alex Paven suggested and @André B got me into VisualTreeHelper class, after several hours of configuring, I finally finished what I needed. Complete code for my question would be:

for (int i = 0; i < VisualTreeHelper.GetChildrenCount(this.spSideNav); i++) // in sidenav stack panel
        {
                DependencyObject current = VisualTreeHelper.GetChild(this.spSideNav, this.currentlyActive + 1);
                if (current.GetType().Equals(typeof(Button)))
                {
                    Button btn = (Button)current;
                    btn.Background = Brushes.DarkBlue;
                    for (int j = 0; j < VisualTreeHelper.GetChildrenCount(current); j++) // in button
                    {
                        current = VisualTreeHelper.GetChild(current, j);
                        if (current.GetType().Equals(typeof(Border)))
                        {
                            for (int k = 0; k < VisualTreeHelper.GetChildrenCount(current); k++) // in border
                            {

                                current = VisualTreeHelper.GetChild(current, k);
                                Border b = (Border)current;
                                b.Background = new System.Windows.Media.SolidColorBrush((Color)ColorConverter.ConvertFromString("#00BAC0")); 
                                ..........
                                more code
                             }
                         }
                      }
                  }
           }

Upvotes: 0

Andr&#233; B
Andr&#233; B

Reputation: 1709

To complete @Alex Paven answer, since this is also something that I've struggled with in the past. I needed to force the ComboBox's ScrollViewer to have zero vertical offset, which was not the case, and was highly uncomfortable when dealing with high size lists. By checking out the template of the ComboBox at the generic.xaml file and by using the VisualTreeHelper class, i could get onto the ScrollViewer and force an event to change it's View.

You have to perform pretty much the same process, but instead you will be referencing your own template, rather than the default one defined in the generic.xaml file.

Just a couple of lines, to help you out.

int count = VisualTreeHelper.GetChildrenCount(myControl);
for (int i = 0; i < count; i++)
{
   DependencyObject current = VisualTreeHelper.GetChild(myControl, i);

   // lets say you have to transverse over a Grid
   if (current.GetType().Equals(typeof(Grid)))
   {
       int count2 = VisualTreeHelper.GetChildrenCount(current);
       for(int k=0; k < count2 ; k++)
       {
          DepedencyObject currentX = VisualTreeHelper.GetChild(current, k)

          .....
          // Keeping transversing the Tree
          .....

          if(currentX.GetType().Equals(typeof(Border))
          {
             Border border = (Border)currentX;
             Border.Background = .... 
          }
       }
   }
}

Upvotes: 1

Alex Paven
Alex Paven

Reputation: 5549

As far as I know, you'll need to navigate the visual tree to find the border, you can't access the contents of a template directly. See https://msdn.microsoft.com/en-us/library/system.windows.media.visualtreehelper(v=vs.110).aspx - using GetChild and GetChildrenCount you can drill down into the visual tree and check for borders until you find the object you want (starting from the button, I imagine). I'm not sure if there's an easier solution (of course you can write your own helper methods or extension methods to make it easier).

Upvotes: 3

Rai Vu
Rai Vu

Reputation: 1635

The following example shows how to set the value of the Background property by using Extensible Application Markup Language (XAML) and code:

myBorder1 = new Border();
myBorder1.BorderBrush = Brushes.SlateBlue;
myBorder1.BorderThickness = new Thickness(5, 10, 15, 20);
myBorder1.Background = Brushes.AliceBlue;
myBorder1.Padding = new Thickness(5);
myBorder1.CornerRadius = new CornerRadius(15);

Upvotes: -2

Related Questions