0

I have a checkbox in my datatemplate and for some reason the events are not firing. see code below. my datatemplate is in a resource dictionary with code behind file. Any Ideas?

<ResourceDictionary x:Class="ArmyBuilder.Templates.EquipmentDataTemplate"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

>

<DataTemplate x:Key="EquipmentDataTemplate">
    <Grid>
        <CheckBox Content="{Binding Name}" Checked="ToggleButton_OnChecked" Click="CheckBox_Click"/>
    </Grid>
</DataTemplate>

 //code behind 
 namespace ArmyBuilder.Templates
{
public partial class EquipmentDataTemplate : ResourceDictionary
{
    public EquipmentDataTemplate()
    {
        InitializeComponent();
    }

    private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)
    {
      // breakpoint not hit
    }

    private void CheckBox_Click(object sender, RoutedEventArgs e)
    {
         // breakpoint not hit
     }
      }
}
shady
  • 1,076
  • 2
  • 13
  • 33
  • Where are you using the DataTemplate? Could you post the XAML? – kkyr Nov 23 '15 at 20:48
  • Are you sure the namespace of your code behind file matches the value of the x:Class attribute? Here is a similar topic: http://stackoverflow.com/a/98422/4424024 – Martin Nov 23 '15 at 22:34
  • @kyriacos_k Yes I am. All the XAML to my data template is in the above. I can see template in my list items, but when I check the checkbox, no event fired. – shady Nov 24 '15 at 13:13
  • @Martin Yes 100% sure. – shady Nov 24 '15 at 13:13

1 Answers1

1

I am not sure how you use it, but the your code works for me and the click event got fired. Check the following and if you still cannot find the point, share a repro project to show how you used it.

Template XAML:

<ResourceDictionary x:Class="App10.EquipmentDataTemplate"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 >

    <DataTemplate x:Key="EquipmentDataTemplate">
        <Grid>
            <CheckBox Content="Click Me" Checked="ToggleButton_OnChecked" Click="CheckBox_Click"/>
        </Grid>
    </DataTemplate>
</ResourceDictionary>

Template cs:

namespace App10
{
    public sealed partial class EquipmentDataTemplate : ResourceDictionary
    {
        public EquipmentDataTemplate()
        {
            this.InitializeComponent();
        }

        private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)
        {
            // breakpoint not hit
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            // breakpoint not hit
        }
    }
}

In MainPage.Xaml, use the template in a ListView:

<Page
    x:Class="App10.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App10"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
        <local:EquipmentDataTemplate></local:EquipmentDataTemplate>
    </Page.Resources>

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView x:Name="listView" CanReorderItems="True" AllowDrop="True" ItemTemplate="{StaticResource EquipmentDataTemplate}">

        </ListView>

    </Grid>
</Page>

In MainPage cs:

namespace App10
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            var list = new ObservableCollection<string>();
            for (int i = 0; i < 10; i++)
            {
                list.Add("Item " + i);

            }
            listView.ItemsSource = list;
        }
    }
}
Alan Yao - MSFT
  • 3,284
  • 1
  • 17
  • 16
  • my resource dictionary comes from a merged dictionary. I put a breakpoint on my resource dictionaries initialize component and it never hit... tried a button too. button shows up but no click event. – shady Nov 25 '15 at 01:10
  • I figured it out. When InitializeComponent wasn't getting called, it led me to the answer. In UserControl Resources I needed . That make the template call InitializeComponent. – shady Nov 25 '15 at 16:50