Just Code‎ > ‎

Silverlight - Let the consumers of your UserControl provide DataTemplate for parts

posted Mar 16, 2010, 2:05 AM by Peter Henell   [ updated Mar 16, 2010, 2:14 AM ]

using System.Windows;
using System.Windows.Controls;

namespace Henell.Silverlight.BeneficiaryReportTool.Config.UserControls
{
public partial class EditAddDeleteList : UserControl
{
public EditAddDeleteList()
{
// Required to initialize variables InitializeComponent(); } DataTemplate _myItemTemplate; public DataTemplate MyItemTemplate
{
get { return _myItemTemplate; }
set
{
_myItemTemplate = value;
CompanyReportList.ItemTemplate = _myItemTemplate;
}
}


public delegate void EditClickEventHandler(object selectedItem);
public delegate void NewClickEventHandler();
public delegate void DeleteClickEventHandler(object selectedItem);

public event EditClickEventHandler OnEditClick;
public event DeleteClickEventHandler OnDeleteClick;
public event NewClickEventHandler OnNewClick;

private void AddNewButton_Click(object sender, RoutedEventArgs e)
{
if(OnNewClick != null)
OnNewClick();
}

private void EditButton_Click(object sender, RoutedEventArgs e)
{
if(OnEditClick != null)
OnEditClick(CompanyReportList.SelectedItem);
}

private void DeleteButton_Click(object sender, RoutedEventArgs e)
{
if(OnDeleteClick != null)
OnDeleteClick(CompanyReportList.SelectedItem);
}
}
}

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:Henell_Silverlight_BeneficiaryReportTool_Config_Behaviors="clr-namespace:Henell.Silverlight.BeneficiaryReportTool.Config.Behaviors" x:Class="Henell.Silverlight.BeneficiaryReportTool.Config.UserControls.EditAddDeleteList" d:DesignWidth="256" d:DesignHeight="326">

<Grid x:Name="LayoutRoot">
<Border>
<StackPanel>

<ListBox x:Name="CompanyReportList" Margin="0" ItemsSource="{Binding}">
<i:Interaction.Behaviors>
<Henell_Silverlight_BeneficiaryReportTool_Config_Behaviors:MouseWheelScrollBehavior/>
</i:Interaction.Behaviors>
</ListBox>
<StackPanel Orientation="Horizontal">
<Button x:Name="AddNewButton" Margin="0" Content="Add new..." Click="AddNewButton_Click" Width="70" />
<Button x:Name="EditButton" Margin="0" Content="Edit..." Click="EditButton_Click" Width="70" />
<Button x:Name="DeleteButton" Margin="0" Content="Delete" Click="DeleteButton_Click" Width="70" />
</StackPanel>
</StackPanel>
</Border>

</Grid>
</UserControl>

Now Consume the usercontrol and provide a DataTemplate for the listitems in the Listbox


<UserControlisar:EditAddDeleteList Name="TheCompanyReportList" DataContext="{Binding CompanyReport}" Grid.Column="2" Grid.Row="2"
                                              >
                <UserControlisar:EditAddDeleteList.MyItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="20" />
                                <RowDefinition Height="16" />
                                <RowDefinition Height="16" />
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" FontSize="18" Text="{Binding CompanyID}"/>
                            <TextBlock Grid.Row="1" FontSize="10" Text="{Binding ReportFileNameStringFormat}"/>
                            <TextBlock Grid.Row="2" FontSize="10" Text="{Binding ReportTemplate.ReportTemplateDescription}"/>
                        </Grid>
                    </DataTemplate>
                </UserControlisar:EditAddDeleteList.MyItemTemplate>
            </UserControlisar:EditAddDeleteList>

You can also hook up to the events of the buttons of course


public MailReceiverGroupConfig()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MailReceiverGroupConfig_Loaded);

TheCompanyReportList.OnEditClick += TheCompanyReportList_OnEditClick;
TheCompanyReportList.OnDeleteClick += TheCompanyReportList_OnDeleteClick;
TheCompanyReportList.OnNewClick += TheCompanyReportList_OnNewClick;
}

void TheCompanyReportList_OnNewClick()
{
ConfigPopups.CompanyReportConfig c = new ConfigPopups.CompanyReportConfig(_ctx, new CompanyReport());
c.Show();
}

void TheCompanyReportList_OnDeleteClick(object selectedItem)
{
MessageBox.Show("Deleting: " + ((CompanyReport)selectedItem).CompanyID);
}

void TheCompanyReportList_OnEditClick(object selectedItem)
{
if (selectedItem != null)
{
ConfigPopups.CompanyReportConfig c = new ConfigPopups.CompanyReportConfig(_ctx, (CompanyReport)selectedItem);
c.Show();
}
}
Comments