1

I have the following XAML code..

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="DataTemplate1" >
        <Border BorderBrush="LightGray" BorderThickness="1" Height="150" Width="500" >
            <Grid Width="500" Height="150" Background="White" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.5*"/>
                    <ColumnDefinition Width="2.5*"/>
                    <ColumnDefinition Width="1*"/>

                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Image Grid.Column="0" Height="Auto" Width="Auto" Source="/Images/notav.jpg" Margin="0,5,4,4" HorizontalAlignment="Left" />
                <TextBlock Text="{Binding PRICE}" TextWrapping="Wrap"  Grid.Column="1"  Width="350"  Foreground="Black" Height="60" Margin="30,85,20,-10"/>

                <TextBlock  Text="{Binding ITMNAME }" FontSize="22" TextWrapping="Wrap" Grid.Column="1"   Name="txtITMNAME" Foreground="DarkBlue" Width="500"  Height="130"  Margin="30,40,20,-10"/>

                <c4f:RoundButton   Grid.Column="2" Name="btntick" Click="btntick_Click" Grid.Row="0" FontSize="25" HorizontalAlignment="Right" Background="LightGray" Foreground="DarkGray" Margin="10,20,45,10"   />

            </Grid>
        </Border>
    </DataTemplate>

<ListBox Height="Auto" Name="lstbxmanual" ItemTemplate="{StaticResource DataTemplate1 }" Width="475" Margin="4,148,0,5" Background="White"  HorizontalAlignment="Left" Grid.RowSpan="2">
</ListBox>

I have to access round button at code behind ,to change its background property...

private void btntick_Click(object sender, RoutedEventArgs e)
{
    btntick. //not  able  to  access...
}

I have gone through following stackoverflow questions..

Access DataTemplate controls in code behind

MSDN link

it seems to be not relevant to my requirement.. please help me in this regard...

Community
  • 1
  • 1
Kartiikeya
  • 2,358
  • 7
  • 33
  • 68
  • I answered a question recently that was like this question. To get a button inside a `DataTemplate` and change it's properties in code by using `FrameworkTemplate.FindName` Method. Check it here and let me know if it was what you wanted: http://stackoverflow.com/questions/34117944/listbox-items-return-string-when-datatemplate-is-button – Salah Akbari Dec 15 '15 at 21:37

2 Answers2

0

Solution 1: Use x:Name instead of Name.

Solution 2: Here is the an alternate way too.

You can cast the event sender object:

private void btntick_Click(object sender, RoutedEventArgs e)
{
    RoundButton rdbtn = sender as RoundButton;
    //rdbtn.BackColor
}
Shaharyar
  • 12,254
  • 4
  • 46
  • 66
  • solution 2 wont work if we want to access all controls inside the listbox...please provide a flexible solution... – Kartiikeya Jul 23 '15 at 08:44
0

please try, x:Name in place of Name it will be accessible from your code behind file.

<c4f:RoundButton   Grid.Column="2" x:Name="btntick" Click="btntick_Click" Grid.Row="0" FontSize="25" HorizontalAlignment="Right" Background="LightGray" Foreground="DarkGray" Margin="10,20,45,10"   />



private void btntick_Click(object sender, RoutedEventArgs e)
    {
       btntick.Foreground = Brushes.Blue;                    
    }

Good Luck.

DeshDeep Singh
  • 1,817
  • 2
  • 23
  • 43