0

Currently, I have a grid row with 3 columns: text (with padding indent), dash line and icon. I expect the result like this:

result

The problem is I can't fix draw line width between text and icon and it isn't dash line, as below: error

Here is my data template:

  <DataTemplate x:Key="TreeItemTemplate">
        <Grid Width="456">
            <Grid.RowDefinitions>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="6*"/>
                <ColumnDefinition Width="3*"/>
                <ColumnDefinition Width="1*"/>
            </Grid.ColumnDefinitions>
            <!-- Text -->
            <TextBlock Margin="0,0,1,0" Visibility="{Binding NormalTextVisible}" TextWrapping="Wrap" Text="{Binding Name, Mode=TwoWay}" d:LayoutOverrides="Width, Height" Foreground="{Binding Colour}" Padding="{Binding ActualIndent, Mode=TwoWay}" FontSize="{StaticResource PhoneFontSizeMedium}"/>

            <!-- Line-->
            <Path  Grid.Column="1" Stroke="Red" StrokeThickness="2" StrokeDashArray="1 2 3" Stretch="Fill">
                <Path.Data>
                    <LineGeometry StartPoint="0,1" EndPoint="1,1" />
                </Path.Data>
            </Path>

            <!-- Icon-->
            <Grid Grid.Column="2" HorizontalAlignment="Right" >
                <Button Style="{StaticResource IconButton}"  Height="42" Width="42" HorizontalAlignment="Right">
                    <ImageBrush ImageSource="{Binding Image}" Stretch="None"/>
                </Button>
            </Grid>
        </Grid>
    </DataTemplate>

And my listbox:

   <ListBox x:Name="lstTreeView" HorizontalAlignment="Stretch" VerticalAlignment="Top" ItemsSource="{Binding TreeViewModel.TreeData, Mode=TwoWay}" ItemTemplate="{StaticResource TreeItemTemplate}"/>

Is there any way to achieve that?

Update: @Ivan Crojach Karačić: Thanks for your help. I tried your way, it worked. But the problem is when I set the first column width as auto, the long text can't wrap in a new line and icon is disappeared (without setting auto, text can wrap normally). You can see the row "Lennin House,..." from this image:

e

Can you fix it? Thanks in advance.

Update 2: Here is my try with wrap text issue

<DataTemplate x:Key="TreeItemTemplate">
<Grid Width="456">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="42"/>
    </Grid.ColumnDefinitions>
    <!-- Text -->
    <Grid Grid.Column="0">
        <TextBlock HorizontalAlignment="Stretch" Margin="0,0,1,0" VerticalAlignment="Center" Visibility="{Binding NormalTextVisible}" TextWrapping="Wrap" Text="{Binding Name, Mode=TwoWay}" d:LayoutOverrides="Width, Height" Foreground="{Binding Colour}" Padding="{Binding ActualIndent, Mode=TwoWay}" FontSize="{StaticResource PhoneFontSizeMedium}"/>
    </Grid>
    <!-- Line-->
    <Line X1="0" Y1="21" X2="500" Y2="21" Stroke="Red" StrokeDashArray="4,1" StrokeThickness="2" Margin="3" Grid.Column="1" VerticalAlignment="Center" Height="42"/>

    <!-- Icon-->
    <Grid Grid.Column="2" HorizontalAlignment="Right" >
        <Button Style="{StaticResource IconButton}"  Height="42" Width="42" HorizontalAlignment="Right">
            <ImageBrush ImageSource="{Binding Image}" Stretch="None"/>
        </Button>
    </Grid>
</Grid>

2 Answers 2

5

As far as I can see your column definitino is the cause for the problem. You allocate 6/10 for the text 3/10 for the line and 1/10 for the icon.

Try it like this

<Grid.ColumnDefinitions>
     <ColumnDefinition Width="Auto"/>
     <ColumnDefinition Width="*"/>
     <ColumnDefinition Width="42"/>
</Grid.ColumnDefinitions>

And you don't need the rowdefinition

The full list of changes could be something like this

<DataTemplate x:Key="TreeItemTemplate">
    <Grid Width="456">
        <Grid.ColumnDefinitions>
            <!--<ColumnDefinition Width="Auto"/>-->
            <ColumnDefinition Width="Auto" MaxWidth="200"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="42"/>
        </Grid.ColumnDefinitions>
        <!-- Text -->
        <TextBlock Margin="0,0,1,0" VerticalAlignment="Center" Visibility="{Binding NormalTextVisible}" TextWrapping="Wrap" Text="{Binding Name, Mode=TwoWay}" d:LayoutOverrides="Width, Height" Foreground="{Binding Colour}" Padding="{Binding ActualIndent, Mode=TwoWay}" FontSize="{StaticResource PhoneFontSizeMedium}"/>

        <!-- Line-->
        <Line X1="0" Y1="21" X2="500" Y2="21" Stroke="Red" StrokeDashArray="4,1" StrokeThickness="2" Margin="3" Grid.Column="1" VerticalAlignment="Center" Height="42"/>

        <!-- Icon-->
        <Grid Grid.Column="2" HorizontalAlignment="Right" >
            <Button Style="{StaticResource IconButton}"  Height="42" Width="42" HorizontalAlignment="Right">
                <ImageBrush ImageSource="{Binding Image}" Stretch="None"/>
            </Button>
        </Grid>
    </Grid>
</DataTemplate>

Edit: If you want the text to wrap you have to make create another grid with the textblock in one row and the listbox in another. Set the texblock horizontal alignment to strech and everything should wrap nicely

Edit: Ah... now I get it...sorry about that. Just add a MaxWidth to the first column definition (see above)

5
  • thanks for your help. It can work, but it give me another problem. Please take a look in my update question. I'm so happy if you can fix it.
    – ductran
    Commented Jun 11, 2013 at 7:52
  • The problem is the long text is belong to listbox's data template, it isn't separate (see <!-- Text --> part). As you can see, the "lennin house", "External".. are the same textblock. So, I can't make a grid view for textblock and listbox. Please take a look my update question. Is there any way solve this? Thanks in advance.
    – ductran
    Commented Jun 11, 2013 at 10:48
  • I highly doubt that "Lennin house,Manway close, Handsworth W..." and "external are the same texblock because on on your picture is red and the other is green which isn't quite possible as far as I know. This "Lennin..." must be some other textblock Commented Jun 11, 2013 at 11:12
  • No, it's really the same. In the first and second image, I crop image without root node (it's my mistake, sorry about that). I have update these pictures, you can check it in my question. You can see this list box is a tree view, and "Lennin house" is a root node, the color and padding (indent) have been binding from model. My view don't have any control except 1 listbox (display as treeview) with this data template. Could you help me again with this issue? Thanks in advance.
    – ductran
    Commented Jun 12, 2013 at 4:14
  • Ok, I tried many ways and found that I just set max width for this text block and everything is fine now. Thanks for your help very much.
    – ductran
    Commented Jun 13, 2013 at 3:30
0

you can have dashed lines using "StrokeDashArray" propert. as bellow. here 2 is filled area and 1 is the gap.

<Line X1="0" Y1="0" X2="100" Y2="0" Stroke="Red" StrokeThickness="5" StrokeDashArray="2,1"></Line>

You can even give StrokeDashArray="1 0.3 4 0.8" . This gives un even spacing in the proportions of 1,0.3,4,0.8 again 1,0.3,4.0.8 and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.