locked
not able to access Button from code

    Question

  • Dear team, 

                    i am not able to access the button from the code.

    <FlipView.ItemTemplate>
                    <DataTemplate>
                        <UserControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates">
                            <ScrollViewer x:Name="scrollViewer" Style="{StaticResource VerticalScrollViewerStyle}" Grid.Row="1">
    
                                <!-- Three-column grid for item-detail layout -->
                                <Grid Margin="120,0,20,20" Name="testing">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="400" />
                                        <ColumnDefinition Width="40" />
                                        <ColumnDefinition Width="360" />
                                        <ColumnDefinition Width="40" />
                                        <ColumnDefinition />
                                    </Grid.ColumnDefinitions>
    
                                    <StackPanel Orientation="Vertical" Grid.Column="0">
                                        <TextBlock FontSize="26.667" FontWeight="Light" Text="{Binding Title}" TextWrapping="Wrap"/>
                                        <Image x:Name="image" Width="400" Margin="0,20,0,10" Stretch="Uniform" Source="{Binding Image}"/>
                                        <StackPanel Orientation="Vertical">
                                            <TextBlock FontSize="26.667" FontWeight="Light" Text="About The Topper" Foreground="BlueViolet" Margin="0,0,0,16"/>
                                            <!--<TextBlock FontSize="26.667" FontWeight="Light" Text=""/>-->
                                            <TextBlock FontSize="26.667" FontWeight="Light" Text="{Binding PrepTime}" Margin="8,0,8,0"/>
                                            <!--<TextBlock FontSize="26.667" FontWeight="Light" Text=""/>-->
                                           
                                        </StackPanel>
                                    </StackPanel>
    
                                    <StackPanel Orientation="Vertical" Grid.Column="2">
                                        <Button x:Name="btnstartexam" IsEnabled="True"  HorizontalAlignment="Center" FontWeight="Light" Content="Start Test" FontSize="26.667" Click="StartTestButtonClicked" />
                                        <TextBlock FontSize="20" FontWeight="Light" LineHeight="32.5" Text="{Binding Ingredients, Converter={StaticResource ListConverter}}" TextWrapping="Wrap" />
                                    </StackPanel>
                                    <StackPanel Orientation="Vertical" Grid.Column="4">
                                        <TextBlock FontSize="26.667" FontWeight="Light" Text="About the Exam" Margin="0,0,0,16"/>
                                        <ScrollViewer Style="{StaticResource VerticalScrollViewerStyle}">
                                            <Grid>
                                                <TextBlock FontSize="20" FontWeight="Light" Text="{Binding Directions}" TextWrapping="Wrap" />
                                            </Grid>
                                        </ScrollViewer>
                                    </StackPanel>
                                </Grid>
    
                               
                            </ScrollViewer>
                        </UserControl>
                    </DataTemplate>
    
                </FlipView.ItemTemplate>
            </FlipView>

     i like to enable or disable based upon the results.

    kindly help

    Saturday, November 08, 2014 8:26 PM

Answers

  • Since you are handling the Loaded event of the UserControl in the ItemTemplate of the FlipView, you can get a reference to the Button in the event handler using the FindName method:

            private void StartLayoutUpdates(object sender, RoutedEventArgs e)
            {
                UserControl uc = sender as UserControl;
                Button btnstartexam = uc.FindName("btnstartexam") as Button;
                //var itemInItemsSource = uc.DataContext as YourType;
                //btnstartexam.IsEnabled = itemInItemsSource.YourProperty;
                btnstartexam.IsEnabled = true;
            }

    The other option is to bind the IsEnabled property of the Button to a boolean source property of the object in the ItemsSource of the FlipView directly in the XAML markup:

    <Button x:Name="btnstartexam" IsEnabled="{Binding YourProperty}"  HorizontalAlignment="Center" FontWeight="Light" Content="Start Test" FontSize="26.667" />

    In the above sample codes, the ItemsSource property of the FlipView is set or bound to some IEnumerable<YourType> collection where YourType is a class that has a boolean property called "YourProperty".

    Please remember to mark helpful posts as answer and/or helpful.

    • Proposed as answer by Bill ChungMVP Sunday, November 09, 2014 5:08 PM
    • Marked as answer by mann madhan1 Thursday, November 13, 2014 1:21 PM
    Sunday, November 09, 2014 4:16 PM

All replies

  • Right. You can't directly access an element in a data template by name: there are likely multiple copies of the template so the name won't be unique.

    The best way to turn your control on and off is probably to data bind the IsEnabled property to some status on the data object. What specifically to bind to will depend on your app logic

    Saturday, November 08, 2014 9:16 PM
    Owner
  • Since you are handling the Loaded event of the UserControl in the ItemTemplate of the FlipView, you can get a reference to the Button in the event handler using the FindName method:

            private void StartLayoutUpdates(object sender, RoutedEventArgs e)
            {
                UserControl uc = sender as UserControl;
                Button btnstartexam = uc.FindName("btnstartexam") as Button;
                //var itemInItemsSource = uc.DataContext as YourType;
                //btnstartexam.IsEnabled = itemInItemsSource.YourProperty;
                btnstartexam.IsEnabled = true;
            }

    The other option is to bind the IsEnabled property of the Button to a boolean source property of the object in the ItemsSource of the FlipView directly in the XAML markup:

    <Button x:Name="btnstartexam" IsEnabled="{Binding YourProperty}"  HorizontalAlignment="Center" FontWeight="Light" Content="Start Test" FontSize="26.667" />

    In the above sample codes, the ItemsSource property of the FlipView is set or bound to some IEnumerable<YourType> collection where YourType is a class that has a boolean property called "YourProperty".

    Please remember to mark helpful posts as answer and/or helpful.

    • Proposed as answer by Bill ChungMVP Sunday, November 09, 2014 5:08 PM
    • Marked as answer by mann madhan1 Thursday, November 13, 2014 1:21 PM
    Sunday, November 09, 2014 4:16 PM