Search code examples
c#.netwpfxamldatatemplate

How to create DataTemplate with Links


I'm trying to create DataTemplates for a PropertyCollection (of PropertyDescriptors) to display the items in the collection in this format,

descriptor1 > descriptor2 > descriptor3 > descriptor4 > descriptor5

Each descriptor would be a link (using Hyperlink), my question is can I do this with Labels (which is not an ItemsControl host)? If yes, can anyone give me an example on how to achieve this with DataTemplates?

Also, is there anyway to access the property descriptor from the DataTemplate? For example suppose I want to use the current instance of property descriptor as the CommandParameter.

Appreciate any help, thanks.


Solution

  • you can try to do this with a ListBox with an ItemsPanel that uses a horizontal layout for the items (I'm just binding to a list of strings in my example). HTH.

    Code:

    public List<string> Properties { get; set; }
    

    XAML:

    <ListBox ItemsSource="{Binding Properties}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
                                ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}"
                                MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}"
                                ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock>
                    <Hyperlink NavigateUri="{Binding}">
                        <TextBlock Text="{Binding StringFormat={}{0} >}"/>
                    </Hyperlink>
                </TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    Screenshot:

    alt text