none
Include Button Click Event in WPF Resource dictionary's CodeBehind file

    Question

  • I have 3 buttons as a part of menu in my MainWindow.xaml file.  
    On click of these I have a Common Grid that gets populated with the
     data from my database. Now for these i have set various Control
     templates with 3 different keys for my 3 different options.

    Coming to my main point. When I include Click events under my
     Gridview and compile, it doesn't let me to do so cause of error.
     And to overcome this i made use of a Code behind C# Class file for
     my resourcedictionary (styles.xaml) that is styles.xaml.cs.
    I got this from http://stackoverflow.com/questions/92100/is-it-possible-to- set-code-behind-a-resource-dictionary-in-wpf-for-event-handling

    But As soon as i follow all procedure mentioned at above link it
    says me ... Error Message: 'X' does not contain a definition for 'show'
     and no extension method 'show' accepting a first argument of
    type 'X' could be found (are you missing a using directive or an
    assembly reference?) Of course I set the Class Attribute in the .xaml file. I
     followed everyhing what was written.
    Still Cannot overcome this issue.  Here is my Code in Styles.xaml resource dictionary.

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         x:Class="Demo.styles"
         x:ClassModifier="public">
    
    <ControlTemplate x:Key="lictemplate" TargetType="{x:Type ListView}">
      <ListView ItemsSource="{Binding}">
       <ListView.View>
        <GridView>
         <GridViewColumn Header="Customer ID" DisplayMemberBinding="{Binding Path=CustomerId}" />
         <GridViewColumn Header="Serial No" DisplayMemberBinding="{Binding Path=SerialNo}"/>
         <GridViewColumn Header="IssueDate" DisplayMemberBinding="{Binding Path=IssueDate, StringFormat=d}"/>
         <GridViewColumn Header="ExpiryDate" DisplayMemberBinding="{Binding Path=ExpiryDate, StringFormat=d}" />
         <GridViewColumn Header="Users" DisplayMemberBinding="{Binding Path=Users}"/>
         <GridViewColumn Header="Type" DisplayMemberBinding="{Binding Path=Type}"/>
         <GridViewColumn Header="Status" DisplayMemberBinding="{Binding Path=Status}"/>     
         <GridViewColumn>
          <GridViewColumn.CellTemplate>
           <DataTemplate>
            <Button Content="Details" Click="OnDetailsClick"/>        
           </DataTemplate>
          </GridViewColumn.CellTemplate>
         </GridViewColumn>
        </GridView>
       </ListView.View>
      </ListView>
     </ControlTemplate>
    

     And Here is Code Behind file of my Resource Dictionary:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    
    namespace Demo
    {
      /// <summary>
    	/// Interaction logic for styles.xaml
    	/// </summary>
      public partial class styles : ResourceDictionary
      {
        public styles()
        {
          InitializeComponent();
    
          // Insert code required on object creation below this point.
        }   
    
        private void OnDetailsClick(object sender, RoutedEventArgs e)
        {
          MessageBox.Show("test ok");
        }    
      }
    }
    Without CLICK events .. Application runs fine...
     But the moment the Click
    event is considered, It show me the Error I said above

    Please suggest me the Fix for this problem. Thanks in Advance.


    Wednesday, October 20, 2010 10:05 AM

Answers

  • Hi Nishit Gandhi

    First, according to your description of the problem, your error message is

     'X' does not contain a definition for 'show'and no extension method 'show' accepting a first argument of type 'X' could be found (are you missing a using directive or an assembly reference?)"

    this message error I cannot reproduce it, after i built your environment, this error missed. I think this error is not  vital.

    I run your source, it works when I delete OnDetailsClick, but if i add this handle, I get an exception "Object reference not set to an instance of an object." 

    when i use tool to debug your source I got message as below:

    Exception object: 0247fb0c
    Exception type:   System.NullReferenceException
    Message:          Object reference not set to an instance of an object.
    InnerException:   <none>
    StackTrace (generated):
        SP       IP       Function
        0037CC3C 001F067E TestApplication!TestApplication.ForResourceDictionary.System.Windows.Markup.IStyleConnector.Connect(Int32, System.Object)+0x7e
        0037CC5C 55402044 PresentationFramework_ni!System.Windows.FrameworkTemplate.LoadTemplateXaml(System.Xaml.XamlReader, System.Xaml.XamlObjectWriter)+0x220

    StackTraceString: <none>
    HResult: 80004003 

    After analysis this message, I find the function "IStyleConnector.Connect" throw the exception when LoadTemplateXaml.

    So in ResourceDictionary you cannot handle events in template. but you can implented handles in other way like this:

    method 1(workable&recommendation):

    you can use commandBinding to add click to your button, and complete your handle in your commands. you can follow this link to finish.

    http://anoriginalidea.wordpress.com/2007/05/23/responding-to-events-from-datatemplate-controls-in-wpf/

    method 2(workable)

    the cause of your original application doesnot work is the app havenot initialized the template before running your source.so you can do some actions like:

    1 remove click-event from your button

    2 after your app load your template, you get the button through VistualTree and add button click event Dynamically.

    They are both workable I think, fist one is better.

    Hope it helps!

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best regards,

    Sheldon _Xiao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Friday, October 22, 2010 5:13 AM
    Moderator
  • Hi Nishit Gandhi

    I have been concerned about your problem.

    have you resolved your problem?

    could you tell me your situation of the problem, I will do my best to resolve your problem.

    Look forward your reply.

     

    Regards,

    Sheldon _Xiao 



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by Nishit Gandhi Wednesday, June 08, 2011 10:22 AM
    Thursday, October 28, 2010 1:41 AM
    Moderator

All replies

  • Hi Nishit Gandhi

    First, according to your description of the problem, your error message is

     'X' does not contain a definition for 'show'and no extension method 'show' accepting a first argument of type 'X' could be found (are you missing a using directive or an assembly reference?)"

    this message error I cannot reproduce it, after i built your environment, this error missed. I think this error is not  vital.

    I run your source, it works when I delete OnDetailsClick, but if i add this handle, I get an exception "Object reference not set to an instance of an object." 

    when i use tool to debug your source I got message as below:

    Exception object: 0247fb0c
    Exception type:   System.NullReferenceException
    Message:          Object reference not set to an instance of an object.
    InnerException:   <none>
    StackTrace (generated):
        SP       IP       Function
        0037CC3C 001F067E TestApplication!TestApplication.ForResourceDictionary.System.Windows.Markup.IStyleConnector.Connect(Int32, System.Object)+0x7e
        0037CC5C 55402044 PresentationFramework_ni!System.Windows.FrameworkTemplate.LoadTemplateXaml(System.Xaml.XamlReader, System.Xaml.XamlObjectWriter)+0x220

    StackTraceString: <none>
    HResult: 80004003 

    After analysis this message, I find the function "IStyleConnector.Connect" throw the exception when LoadTemplateXaml.

    So in ResourceDictionary you cannot handle events in template. but you can implented handles in other way like this:

    method 1(workable&recommendation):

    you can use commandBinding to add click to your button, and complete your handle in your commands. you can follow this link to finish.

    http://anoriginalidea.wordpress.com/2007/05/23/responding-to-events-from-datatemplate-controls-in-wpf/

    method 2(workable)

    the cause of your original application doesnot work is the app havenot initialized the template before running your source.so you can do some actions like:

    1 remove click-event from your button

    2 after your app load your template, you get the button through VistualTree and add button click event Dynamically.

    They are both workable I think, fist one is better.

    Hope it helps!

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best regards,

    Sheldon _Xiao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Friday, October 22, 2010 5:13 AM
    Moderator
  • Hi Nishit Gandhi

    I have been concerned about your problem.

    have you resolved your problem?

    could you tell me your situation of the problem, I will do my best to resolve your problem.

    Look forward your reply.

     

    Regards,

    Sheldon _Xiao 



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by Nishit Gandhi Wednesday, June 08, 2011 10:22 AM
    Thursday, October 28, 2010 1:41 AM
    Moderator
  • Thanks a lot, Sheldon your 1st and recommended solution works. Sorry for late reply but since I was new to MSDN at that time , I did not know that we have to mark the replies as answer. Thanks again.
    Wednesday, June 08, 2011 10:29 AM
  • Thank you for your feedback.
    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 08, 2011 10:35 AM
    Moderator