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

时间:2010-07-09 09:28:41

标签: templates custom-controls binding

我在自定义控件中使用数据绑定

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

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中的keyword属性。

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>

如果有人能帮我这个......

1 个答案:

答案 0 :(得分:0)

在您的情况下,PageDirectory类需要实现INotifyPropertyChanged接口。每次关键字内部更改时,您都需要像下面这样引发一个PropertyChanged事件:

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

然后绑定字符串将具有如下视图:

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

其中local是在xaml中声明为upper的名称空间,用于访问PageDictionary类型。