2010-07-09 93 views
0

我与数据绑定在一个自定义的控制自定义控制

我我自定义的控制范围内得到了一些性质stuggeling与属性中绑定到一个集合。

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> 

如果有人可以帮我这个...

回答

0

在你的情况PageDirectory类需要实现INotifyPropertyChanged接口。每次关键字里面也改变了你需要引发PropertyChanged事件是这样的:

if (PropertyChanged != null) 
PropertyChanged(this, new PropertyChangedEventArgs("Keywords")) 

捆扎带届时将有鉴于这样的:

{Binding Keywords, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:PageDictionary}}} 

如果当地的命名空间中声明上在XAML为访问PageDictionary类型。