Hank
Hank

Reputation: 2616

How to style rotated TextBlocks closer together?

When I manually set these TextBlocks up I was able to space them out nicely by altering the left margin to -50, except for the first one which got set to 0(zero). enter image description here

I am now get the TextBlocks populated through binding, so when I apply a style it happens to all TextBlocks.

<Style x:Key="RotatedText" TargetType="TextBlock">
    <Setter Property="LayoutTransform">
        <Setter.Value>
            <RotateTransform Angle="-45" />
        </Setter.Value>
    </Setter>
    <Setter Property="Width" Value="130"/>
    <Setter Property="Margin" Value="-50,0,0,0"/>
</Style>

Now this happens: enter image description here

What I am wondering is how can I create either a style that works for all TextBlocks or define a separate style for the first TextBlock and another for the rest.

<ListBox x:Name="lstModules" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.Modules}" BorderBrush="{x:Null}" Background="{x:Null}" BorderThickness="0">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" CanVerticallyScroll="False" CanHorizontallyScroll="False"></StackPanel>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Style="{StaticResource ListViewItemRotatedText}" Text="{Binding ModuleName}"></TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Upvotes: 1

Views: 134

Answers (2)

Hank
Hank

Reputation: 2616

I have ended up using an IValueConverter to analyze the item to see if it is the first item in the list.

<Style.Triggers>
    <DataTrigger Binding="{Binding ModuleName, Converter={StaticResource firstItemConvertion}}" Value="true">
        <Setter Property="Margin" Value="-60,0,0,0" />
    </DataTrigger>
</Style.Triggers>


public class FirstItemConverter : IValueConverter
{
    public object Convert(object obj, Type type, object parameter, CultureInfo culture)
    {
        return (int.Parse((string)parameter)) < ModuleRepository.GetModuleIndexByModuleName(((string)obj));
    }

    public object ConvertBack(object obj, Type type, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Upvotes: 0

torrential coding
torrential coding

Reputation: 1765

You could just move the whole ListBox by giving it a left margin of 50. This way all the TextBlocks that are contained in the ListBox will get shifted as desired.

Upvotes: 1

Related Questions