locked
Provide access to Custom Control Method RRS feed

  • Question

  • I am building a custom control, whose inner controls template can be altered by user of control.

    I have written a standard method in that custom control and I want to give user ability to add that standard method as event handler of controls in template if that method matches event handler type for that control event.

    Is it even possible? 

    Saturday, June 28, 2014 11:28 AM

Answers

  • Hi,

    I do not quite understand your problem. And I have a question about your code. How do you  apply the DataTemplate which define in the MainPage to listview in a usercontrol. Would you mind sharing a reproduce example about it to oneDrive? And If you define a DataTemplate in a page and set the checkbox control checked event, I think you cannot add the eventhandler which define in a usercontrol to the checkbox. why don't you add the dataTemplate in a usercontrol resource.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.


    • Edited by Anne Jing Tuesday, July 1, 2014 3:41 AM edit
    • Marked as answer by Talha.Ahmed Tuesday, July 1, 2014 9:07 AM
    Tuesday, July 1, 2014 3:40 AM

All replies

  • Hi Talha,

    I don't get what you want to do. It would be helpful to have some code that shows what you're doing and where the problem is. :)


    Thomas Claudius Huber

    "If you can't make your app run faster, make it at least look & feel extremly fast"

    My latest app: The "Womanizer" :-)
    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook

    Saturday, June 28, 2014 7:55 PM
  • Hi Talha,

    I don't get what you want to do. It would be helpful to have some code that shows what you're doing and where the problem is. :)


    Thomas Claudius Huber

    "If you can't make your app run faster, make it at least look & feel extremly fast"

    My latest app: The "Womanizer" :-)
    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook

    <UserControl
        x:Class="CustomControls.CustomListView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:CustomControls"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300"
        d:DesignWidth="400">
    
    
    
        <ListView x:Name="RootListView"
                SelectionMode="Extended"
                CanDragItems="True"
                SelectionChanged="SelectionChanged"
                AllowDrop="True"
                DragItemsStarting="RootListView_DragItemsStarting"
                Drop="DragDropped"
                Header="{Binding Header}"
                HeaderTemplate="{Binding HeaderTemplate}"
                ItemsPanel="{Binding ItemsPanel}" 
                Background="{Binding ActualBackground}"
                ItemsSource="{Binding ItemsSource}"
                Foreground="{Binding ActualForeground}"
                BorderThickness="{Binding ActualBorderThickness}"
                BorderBrush="{Binding ActualBorderBrush}"
                ItemTemplate="{Binding ItemTemplate}"
    ContainerContentChanging="ItemsChanged"
                >
        </ListView>
    </UserControl>

    I have a control like this. To expose the properties of inner control, I used binding. So, the user of CustomListView can change properties of inner ListView by changing CustomListView properties. Now, I have written a EventHandler for "Checkbox.Checked" and "Checkbox.Unchecked" in code behind of CustomListView.(Say EventHandler is named CheckChangedHandler)

    CustomListView can be used like this:

    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:local="using:TimeTable_c_sharp"
          xmlns:Controls="using:CustomControls"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:ProjectClasses="using:ProjectClasses"
          xmlns:UI="using:Microsoft.Advertising.WinRT.UI"
          x:Class="TimeTable_c_sharp.MainPage"
          mc:Ignorable="d">
    
        <Page.Resources>
            <Style TargetType="Controls:CustomListView" x:Key="CListViewStyle1">
                <Setter Property="Height" Value="346" />
                <Setter Property="Width" Value="425" />
                <Setter Property="VerticalAlignment" Value="Top" />
                <Setter Property="HorizontalAlignment" Value="Left" />
                <Setter Property="AllowDrop" Value="True" />
                <Setter Property="ActualBackground" Value="#FF1D2E42" />
                <Setter Property="ActualBorderBrush" Value="#FF575757" />
                <Setter Property="ActualBorderThickness" Value="1" />
            </Style>
    
            <DataTemplate x:Key="AllSubjectsHeaderTemplate">
                <Border BorderBrush="#FF677E7B"
                        BorderThickness="0,0,0,1"
                        Background="#FF534C4C">
                    <StackPanel Orientation="Horizontal">
                        <CheckBox x:Name="checkBox"
                                  HorizontalAlignment="Left"
                                  Margin="10"
                                  VerticalAlignment="Top"
                                  IsChecked="{Binding IsChecked, Mode=TwoWay}"
                                  Width="27" />
    
                        <TextBlock Grid.Column="1"
                                   TextAlignment="Center"
                                   Text="{Binding Content.Column1, RelativeSource={RelativeSource Mode=TemplatedParent}}"
                                   Margin="22,10,0,0"
                                   TextWrapping="Wrap"
                                   FontStyle="Normal"
                                   FontWeight="Bold"
                                   FontSize="18"
                                   VerticalAlignment="Top"
                                   Width="298"
                                   Height="24"
                                   x:Name="Header1"
                                   Foreground="#FFB2BF8F" />
                    </StackPanel>
                </Border>
            </DataTemplate>
        </Page.Resources>
    
            <Controls:CustomListView x:Name="allSubjectsList"
                                     Margin="266,141,0,0"
                                     Header="{Binding AllSubjectsHeader}"
                                     ItemsSource="{Binding AvailibleSubjects}"
                                     Style="{StaticResource CListViewStyle1}"
                                     HeaderTemplate="{StaticResource AllSubjectsHeaderTemplate}"
                                     />
    </Page>


    As you can see, user can change templates of CustomListView->RootListView. Now, there is a checkbox in Datatemplate user applied as HeaderTemplate. I want to give user ability to add following line in that checkbox.

    <CheckBox Checked=<!-- Event Handler for check changed defined in CustomListView code behind -->
    />

    Hope, you understands this.

    Sunday, June 29, 2014 9:11 AM
  • Hi,

    You should register for the CheckBox's Checked event in the UserControl. And in the UserControl you should  also create an API and call the Click event. So, When the CheckBox's Checked event is fired, the UserControl can listens for it and fires this Click event. A simple example below you can refer to:

    UserControl XAML:

    <UserControl
        x:Class="Usercontrolevent.MyUserControl1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Usercontrolevent"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300"
        d:DesignWidth="400" x:Name="mycontrol">
        <UserControl.Resources>
            <DataTemplate x:Key="AllSubjectsHeaderTemplate">
                <Border BorderBrush="#FF677E7B"
                        BorderThickness="0,0,0,1"
                        Background="#FF534C4C">
                    <StackPanel Orientation="Horizontal">
                        <CheckBox x:Name="checkBox"
                                  HorizontalAlignment="Left"
                                  Margin="10"
                                  VerticalAlignment="Top"
                                  IsChecked="{Binding IsChecked}"
                                  Width="27" Checked="checkBox_Checked" />
    
                    </StackPanel>
                </Border>
            </DataTemplate>
        </UserControl.Resources>
        <Grid>
            <ListView ItemTemplate="{Binding Source={StaticResource AllSubjectsHeaderTemplate}}" DataContext="{Binding DataContext,ElementName=mycontrol}" ItemsSource="{Binding}"></ListView>
        </Grid>
    </UserControl>

    UserControl.cs:

    public sealed partial class MyUserControl1 : UserControl
        {
            public MyUserControl1()
            {
                this.InitializeComponent();
    
            }
            public static readonly DependencyProperty ItemTemplateProperty =
        DependencyProperty.Register(
        "ItemTemplate",
        typeof(String),
        typeof(MyUserControl1), null
        );
            public DataTemplate  ItemTemplate
            {
                get { return (DataTemplate)GetValue(ItemTemplateProperty); }
                set { SetValue(ItemTemplateProperty, (DataTemplate)value); }
            }
            public static readonly DependencyProperty ItemsSourceProperty =
       DependencyProperty.Register(
       "ItemsSource ",
       typeof(object),
       typeof(MyUserControl1), null
       );
            public object ItemsSource
            {
                get { return (object)GetValue(ItemTemplateProperty); }
                set { SetValue(ItemTemplateProperty, (object)value); }
    
            }
    
            public event EventHandler Onchecked;
    
    
            private void checkBox_Checked(object sender, RoutedEventArgs e)
            {
                Onchecked(this,null);
    
            }
           
        }

    MainPage XAML:

    <Page.Resources>
            
            <CollectionViewSource x:Name="cvs1" />
        </Page.Resources>
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <local:MyUserControl1  DataContext="{StaticResource cvs1}" ItemsSource="{Binding}" Onchecked="Checkevent"></local:MyUserControl1>
        </Grid>

    MainPage.cs:

     public MainPage()
            {
                this.InitializeComponent();
    
                ObservableCollection<Test> test = new ObservableCollection<Test>();
                test.Add(new Test { IsChecked = true });
                test.Add(new Test { IsChecked = false });
                test.Add(new Test { IsChecked = true });
                test.Add(new Test { IsChecked = false });
                cvs1.Source = test;
    
                
            }
            public void Checkevent(object sender,EventArgs args)
            {
    
            }
           
        }
        public class Test
        {
            public bool IsChecked { get; set; }
        }
       

    And you can refer to the link:

    http://social.msdn.microsoft.com/Forums/en-US/a7f09287-85e9-43a1-ba26-481ffa8d8d9f/how-to-access-usercontrols-uielements-property-in-other-usercontrol-or-page?forum=winappswithcsharp

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Monday, June 30, 2014 8:21 AM
  • Thanks for your response.

    What I am trying to do is expose the event handler not listener. In case of your sample, it should be something like this:

    In UserControl.Xaml

    <UserControl
        x:Class="Usercontrolevent.MyUserControl1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Usercontrolevent"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300"
        d:DesignWidth="400" x:Name="mycontrol">
        
        <Grid>
            <ListView x:Name="listView" ItemTemplate="{Binding ItemTemplate}" ItemsSource="{Binding ItemsSource}"></ListView>
        </Grid>
    </UserControl>

    In UserControls.cs

    public sealed partial class MyUserControl1 : UserControl
        {
            public MyUserControl1()
            {
                this.InitializeComponent();
    	    this.listView.DataContext=this;
            }
            public static readonly DependencyProperty ItemTemplateProperty =
        DependencyProperty.Register(
        "ItemTemplate",
        typeof(String),
        typeof(MyUserControl1), null
        );
            public DataTemplate  ItemTemplate
            {
                get { return (DataTemplate)GetValue(ItemTemplateProperty); }
                set { SetValue(ItemTemplateProperty, (DataTemplate)value); }
            }
            public static readonly DependencyProperty ItemsSourceProperty =
       DependencyProperty.Register(
       "ItemsSource ",
       typeof(object),
       typeof(MyUserControl1), null
       );
            public object ItemsSource
            {
                get { return (object)GetValue(ItemTemplateProperty); }
                set { SetValue(ItemTemplateProperty, (object)value); }
            }
    
            public void HandlerIWantToExpose(object sender, RoutedEventArgs e)
            {
                //Do Something
            }
           
        }

    In MainPage.Xaml

    
    
    <Page.Resources>
            
            <CollectionViewSource x:Name="cvs1" />
            <DataTemplate x:Key="AllSubjectsHeaderTemplate">
                <Border BorderBrush="#FF677E7B"
                        BorderThickness="0,0,0,1"
                        Background="#FF534C4C">
                    <StackPanel Orientation="Horizontal">
                        <CheckBox x:Name="checkBox"
                                  HorizontalAlignment="Left"
                                  Margin="10"
                                  VerticalAlignment="Top"
                                  IsChecked="{Binding IsChecked}"
                                  Width="27" Checked="[HandlerExposedByMyUserControl1]" />
                    </StackPanel>
                </Border>
            </DataTemplate>
        </Page.Resources>
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <local:MyUserControl1 ItemTemplate="{StaticResource AllSubjectsHeaderTemplate}" ItemsSource="{StaticResource cvs1}"></local:MyUserControl1>
        </Grid>

    MainPage.cs
    public MainPage()
            {
                this.InitializeComponent();
    
                ObservableCollection<Test> test = new ObservableCollection<Test>();
                test.Add(new Test { IsChecked = true });
                test.Add(new Test { IsChecked = false });
                test.Add(new Test { IsChecked = true });
                test.Add(new Test { IsChecked = false });
                cvs1.Source = test;
    
                
            }
        }
        public class Test
        {
            public bool IsChecked { get; set; }
        }

    Monday, June 30, 2014 10:44 AM
  • Hi,

    I do not quite understand your problem. And I have a question about your code. How do you  apply the DataTemplate which define in the MainPage to listview in a usercontrol. Would you mind sharing a reproduce example about it to oneDrive? And If you define a DataTemplate in a page and set the checkbox control checked event, I think you cannot add the eventhandler which define in a usercontrol to the checkbox. why don't you add the dataTemplate in a usercontrol resource.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.


    • Edited by Anne Jing Tuesday, July 1, 2014 3:41 AM edit
    • Marked as answer by Talha.Ahmed Tuesday, July 1, 2014 9:07 AM
    Tuesday, July 1, 2014 3:40 AM
  • Thanks for specifying that it might not be possible. I will try some different approch then.

    That's how I am applying DataTemplate which define in the MainPage to listview in a usercontrol and it is working fine so far.

    Applying DataTemplate from MainPage to inner UserControl

    Tuesday, July 1, 2014 9:07 AM