Templates 自定义控件绑定到属性内的集合

Templates 自定义控件绑定到属性内的集合,templates,custom-controls,binding,Templates,Custom Controls,Binding,我在自定义控件中遇到了数据绑定问题 我的自定义控件中有一些属性 public static DependencyProperty TitleProperty; public static DependencyProperty PageDictionaryProperty; public string Title { get { return (string)base.GetValue(TitleProperty); } set {

我在自定义控件中遇到了数据绑定问题

我的自定义控件中有一些属性

public static DependencyProperty TitleProperty;
public static DependencyProperty PageDictionaryProperty;

public string Title
{
    get
    {
        return (string)base.GetValue(TitleProperty);
    }
    set
    {
        base.SetValue(TitleProperty, value);

    }

}

public Innax.ManualParts.PageDictionary PageDictionary
{
    get
    {
        return (Innax.ManualParts.PageDictionary)base.GetValue(PageDictionaryProperty);
    }
    set
    {
        base.SetValue(PageDictionaryProperty, value);
    }

}
绑定到表不是问题。这个很好用

但是现在我想绑定到pageDictionary中的关键字属性

public class PageDictionary
{
    //some other properties....
    public List<string> Keywords
    {
        get
        {
            return GetKeyWords();
        }
    }
}
这是XAML文件

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:InnaxBook" >
    <Style x:Key="alternatingWithTriggers" 
               TargetType="{x:Type ContentPresenter}">
        <Setter Property="Height" Value="25"></Setter>
    </Style>
    <DataTemplate x:Key="HelpTopicLineTemplate">
        <Border x:Name="HelpTopicLine">
            <StackPanel Orientation="Horizontal">
                <Button Content="{Binding DictionaryName}" Width="25px" x:Name="btnGoto" ></Button>
            </StackPanel>
        </Border>
    </DataTemplate>
    <Style TargetType="{x:Type local:Manual}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:Manual}" >
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <Grid Height="Auto" Width="Auto">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="125" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="30" />
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="30" />
                                </Grid.RowDefinitions >
                                <Label x:Name="TitleControl" Content="{TemplateBinding Title }"  Grid.ColumnSpan="2" Grid.Column="0" Grid.Row="0" Background="Aqua" ></Label>
                                <StackPanel x:Name="IndexContainer" Grid.Column="0" Grid.Row="1" Height="Auto" Width="Auto"  Orientation="Vertical" ScrollViewer.CanContentScroll="True">
                                    <Label Height="30" Margin="0,0,0,0" HorizontalAlignment="Left" Content="{TemplateBinding IndexLabelText}" />
                                    <ListView  HorizontalAlignment="Stretch">
                                        <Button Height="Auto" HorizontalContentAlignment="Stretch" Content="knoppie" />
                                    </ListView>

                                    <ItemsControl ItemContainerStyle="{StaticResource alternatingWithTriggers}" 
                                            AlternationCount="2" 
                                            x:Name="RelatedItemsControl" 
                                            Grid.Row="1"
                                            Grid.Column="0"
                                            ItemsSource="{TemplateBinding PageDictionary}"
                                            ItemTemplate="{StaticResource HelpTopicLineTemplate}">
                                    </ItemsControl>

                                </StackPanel>
                            </Grid>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>
</ResourceDictionary>

如果有人能帮我解决这个问题……

在您的情况下,PageDirectory类需要实现INotifyPropertyChanged接口。每次更改关键字inside时,您都需要引发一个PropertyChanged事件,如下所示:

if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Keywords"))
{Binding Keywords, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:PageDictionary}}}
绑定字符串将具有如下视图:

if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Keywords"))
{Binding Keywords, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:PageDictionary}}}
其中,local是access PageDictionary类型的xaml中声明为upper的命名空间