Master
Master

Reputation: 2153

Setting Itemsource for ControlTemplate

I have created an AutocompleteBox and it works completely fine outside a ControlTemplate. When I place it within a Control template, the autocompletebox is no longer populated with any items.

<ControlTemplate x:Key="EditAppointmentTemplate" TargetType="telerik:SchedulerDialog">
    <Grid Margin="6">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="97" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <TextBlock Text="Participants" Margin="6 0" VerticalAlignment="Center" HorizontalAlignment="Left" />
        <telerik:RadAutoCompleteBox Margin="6 0"
                                    Grid.Column="1"
                                    ItemsSource="{Binding Atts}" 
                                    SelectedItems="{Binding SelectedAttendees,Mode=TwoWay}" 
                                    DisplayMemberPath="DisplayName" 
                                    TextSearchPath="Search" 
                                    Style="{StaticResource MultiAutoBox}" 
                                    WatermarkContent="Search ..." 
                                    MinHeight="55" VerticalContentAlignment="Top" Padding="5">
        </telerik:RadAutoCompleteBox>
    </Grid>
</ControlTemplate>

<Style x:Key="EditAppointmentDialogStyle" TargetType="telerik:SchedulerDialog">
     ....
     <Setter Property="Template" Value="{StaticResource EditAppointmentTemplate}" />
     ....
<Style x:Key="EditAppointmentDialogStyle"/>

<telerik:RadScheduleView x:Name="scheduleview" ....
                         EditAppointmentDialogStyle="{StaticResource EditAppointmentDialogStyle}"
                         ....
 <telerik:RadScheduleView x:Name="scheduleview"/>

I'm thinking I have to set the ItemsSource to target a relative ancestor I tried the following and the itemsource isn't populating still.

ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:SchedulerDialog}}, Path=Atts}"

Upvotes: 0

Views: 190

Answers (1)

brunnerh
brunnerh

Reputation: 184524

Control templates should be entirely self contained, as such your control should expose a dependency property (call it SuggestionsSource for example) to which the auto-complete box binds via a TemplateBinding.

Where you use the dialog control you then bind said property to your DataContext property.


In your dialogue class (if you want to extend the functionality of an existing control you will need a sub-class to introduce properties, here MySchedulerDialog)

public static readonly DependencyProperty SuggestionsSourceProperty =
DependencyProperty.Register("SuggestionsSource", typeof(IList), typeof(MySchedulerDialog), new UIPropertyMetadata(null));
public IList SuggestionsSource
{
    get { return (IList)GetValue(SuggestionsSourceProperty); }
    set { SetValue(SuggestionsSourceProperty, value); }
}

In control template XAML:

<telerik:RadAutoCompleteBox Margin="6 0"
                            Grid.Column="1"
                            ItemsSource="{TemplateBinding SuggestionsSource}" ...>

Where you use the control:

<local:MySchedulerDialog SuggestionsSource="{Bindings Atts}" .../>

Upvotes: 1

Related Questions