1

I just started WPF and I'm getting problem for stylling. I have a style that I use for all TextBlock in the UserControl.

<UserControl.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="Margin" Value="10" />
    </Style>
</UserControl.Resources>

It work great for the TextBlock in the first StackPanel but not for the TextBlock in the TreeView.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition />
    </Grid.RowDefinitions>

    <StackPanel Grid.Row="1" Orientation="Horizontal">
        <TextBlock Text="{Binding Name}" />
        <TextBlock Text="{Binding Description}" />
    </StackPanel>

    <TreeView ItemsSource="{Binding Tests}" Grid.Row="2">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type dml:TestCase}" ItemsSource="{Binding Tasks}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" />
                    <TextBlock Text="{Binding Description}" />
                </StackPanel>
            </HierarchicalDataTemplate>

            <!--Task Template-->
            <HierarchicalDataTemplate DataType="{x:Type dml:Task}">
                <TextBlock Text="{Binding Name}" />
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>
</Grid>

Is there a way to do it without setting style on each TextBlock in the TreeView?

Félix LD
  • 372
  • 1
  • 5
  • 19
  • Sorry I didnt realize the problem please check this link. https://msdn.microsoft.com/en-us/library/system.windows.hierarchicaldatatemplate.itemcontainerstyle(v=vs.110).aspx for TreeViewItem you want to give style then you need to give style for TreeViewItem. – Abin Jul 23 '15 at 15:06
  • All I want is some margin between TextBlock who are in the same StackPanel – Félix LD Jul 23 '15 at 15:09
  • 1
    @felix I keeping my eye on you to see if you encounter issues I can help you with - so far people helped you faster - thanks for repairing my posts ;) – G.Y Aug 09 '15 at 01:37

2 Answers2

1

Just do it in your ItemTemplate:

<HierarchicalDataTemplate>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Name}" Margin="0,0,10,0" />
        <TextBlock Text="{Binding Description}" />
    </StackPanel>
</HierarchicalDataTemplate>
Mathlight
  • 6,436
  • 17
  • 62
  • 107
eran otzap
  • 12,293
  • 20
  • 84
  • 139
1

Ok that here we go,

<TreeView ItemsSource="{Binding Tests}" >
        <TreeView.Resources>
            <HierarchicalDataTemplate ItemsSource="{Binding Tasks}" DataType="Hdtable1">
                <StackPanel Orientation="Horizontal">
                    <StackPanel.Resources>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="Margin" Value="10"/>
                        </Style>
                    </StackPanel.Resources>
                    <TextBlock Text="{Binding Name}" />
                    <TextBlock Text="{Binding Description}" />
                </StackPanel>
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="HDTEmplate">
                <TextBlock Text="{Binding Name}" />
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>

Credits:- https://stackoverflow.com/a/932609/2470362
https://stackoverflow.com/a/3102188/2470362

Community
  • 1
  • 1
Abin
  • 2,868
  • 1
  • 23
  • 59