为ControlTemplate设置Itemsource

时间:2016-05-13 18:54:25

标签: c# wpf xaml telerik

我创建了一个AutocompleteBox,它在ControlTemplate之外完全正常工作。当我将其放在Control模板中时,自动完成框不再填充任何项目。

<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"/>

我想我必须将ItemsSource设置为以相对祖先为目标 我尝试了以下内容,而且itemsource还没有填充。

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

1 个答案:

答案 0 :(得分:1)

控件模板应完全自包含,因此您的控件应公开自动完成框通过SuggestionsSource绑定的依赖项属性(例如,调用它为TemplateBinding

在使用对话框控件的地方,然后将所述属性绑定到DataContext属性。

在你的对话类中(如果你想扩展现有控件的功能,你需要一个子类来引入属性,这里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); }
}

在控制模板XAML中:

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

使用控件的地方:

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