Monday, June 30, 2014

Provide access to Custom Control Method

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://ift.tt/w9rHWX;
xmlns:x="http://ift.tt/zZkFEV;
xmlns:local="using:Usercontrolevent"
xmlns:d="http://ift.tt/PuXO1J;
xmlns:mc="http://ift.tt/R4RR6u;
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; }
}


No comments:

Post a Comment