locked
Referencing Application.Resources elements in C# file.

    Question

  • <Application.Resources>
            <Style TargetType="AppBar"
                   x:Name="testBar">
                <Setter Property="Template" >
                    <Setter.Value>
                        <ControlTemplate TargetType="AppBar">
                        <Grid x:Name="testGrid">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="50*" />
                                <ColumnDefinition Width="50*" />
                            </Grid.ColumnDefinitions>
                            <StackPanel x:Name="LeftPanel"
                                        Orientation="Horizontal"
                                        Grid.Column="0"
                                        HorizontalAlignment="Left">
                                <AppBarButton x:Uid="Camera"
                                              Icon="Camera"
                                              Label="Camera" />
                                <AppBarButton x:Uid="Add"
                                              Icon="Add"
                                              Label="Add" />
                            </StackPanel>
                            <StackPanel x:Name="RightPanel"
                                        Orientation="Horizontal"
                                        Grid.Column="1"
                                        HorizontalAlignment="Right">
                                <AppBarButton x:Uid="Add"
                                              Icon="Add"
                                              Label="Add" />
                                <AppBarButton x:Uid="Remove"
                                              Icon="Remove"
                                              Label="Remove" />
                                <AppBarSeparator />
                                <AppBarButton x:Uid="Delete"
                                              Icon="Delete"
                                              Label="Delete" />
                            </StackPanel>
                        </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                    </Setter>
            </Style>
    </Application.Resources>


    This is the code in placed inside Application.Resources file of App.xaml

    I can set this AppBar style to my Page AppBar easily using:

    AppBar myAppBar = new AppBar();
    myAppBar.Style = (Style)Application.Current.Resources["testBar"];
    this.BottomAppBar = myAppbar;

    But how can I access that AppBarButton with x:Uid="Delete"

    or how can I access StackPanel with x:Name="RightPanel"

    from my .cs file.

    I also want to add a event handler.

    If it had not been a resource file........
    then I would have used :

    StackPanel rSP = new StackPanel();
    rSP = FindName("RightPanel") as StackPanel;

    But this is is giving error when trying on App.xaml file

    So, how to access those elements?

    • Edited by amanmehara Monday, June 09, 2014 7:01 PM
    Monday, June 09, 2014 7:00 PM

Answers

  • You can find the controls placed inside a control template by using VisualTreeHelper class.

    - Ram
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by amanmehara Friday, June 13, 2014 8:09 PM
    Monday, June 09, 2014 7:31 PM
  • Hi amanmehara,

    Yes, a blog from Jerry should helps with using Visual Tree Helper: http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by amanmehara Friday, June 13, 2014 8:09 PM
    Tuesday, June 10, 2014 9:00 AM
    Moderator

All replies

  • You can find the controls placed inside a control template by using VisualTreeHelper class.

    - Ram
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by amanmehara Friday, June 13, 2014 8:09 PM
    Monday, June 09, 2014 7:31 PM
  • Hi amanmehara,

    Yes, a blog from Jerry should helps with using Visual Tree Helper: http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by amanmehara Friday, June 13, 2014 8:09 PM
    Tuesday, June 10, 2014 9:00 AM
    Moderator