【问题标题】:Setting Itemsource for ControlTemplate为 ControlTemplate 设置 Itemsource
【发布时间】:2016-05-13 18:54:25
【问题描述】:

我创建了一个 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}"

【问题讨论】:

    标签: c# wpf xaml telerik


    【解决方案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}" .../>
    

    【讨论】:

    • 能否给我一个 sn-p 或 PseudoCode,以便我更深入地了解您所指的内容?
    • @Master:你真的应该阅读 MSDN 上的控制创作,无论如何添加示例......
    猜你喜欢
    • 2019-01-22
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多