2010-07-09 195 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類型。