WPF Question: Is this possible from mark up?

time to read 12 min | 2259 words

(Image from clipboard).png

"Categories:" is static text, the rest is generated via this code:

IAddChild c = Categories;//Categories is a Stack Panel with Horizontal Orientation

foreach (Category category in post.Categories)

{

      TextBlock block = new TextBlock();

      block.Text = category.Name;

      block.Style = (Style) FindResource("BigText");

      c.AddChild(block);

      TextBlock seperator = new TextBlock();

      seperator.Text = "//";

      seperator.Style = (Style)FindResource("BigText");

      seperator.Foreground = Brushes.Brown;

      c.AddChild(seperator);

}

Is there a way to do this via XAML and not from the code?

I would like to thank Tomas Restrepo for pointing me in the direct direction, this is indeed possible, like this:

<ItemsControl Name="Categories"

                    ItemsSource="{Binding}">

      <ItemsControl.ItemsPanel>

            <ItemsPanelTemplate>

                  <WrapPanel Orientation="Horizontal"/>

            </ItemsPanelTemplate>

      </ItemsControl.ItemsPanel>

      <ItemsControl.ItemTemplate>

            <DataTemplate>

                  <WrapPanel>

                        <TextBlock Text="{Binding Path=Name}" Tag="{Binding Path=CategoryId}"

                                       MouseDown="Category_Browse"

                                       Style="{StaticResource BigText}"/>

                        <TextBlock Text="//"

                                       Foreground="Brown"

                                       Style="{StaticResource BigText}"/>

                  </WrapPanel>

            </DataTemplate>

      </ItemsControl.ItemTemplate>

</ItemsControl>

Notice the ItemsSource="{Binding}", which tripped me at first. Also, note that I am binding the tag of the text blog to the ID of the category. I don't really like this, but I couldn't figure out how to bind to the current item itself.