简体   繁体   English

如何在网格行Windows Phone中绘制虚线

[英]How to draw dash line in grid row windows phone

Currently, I have a grid row with 3 columns: text (with padding indent), dash line and icon. 目前,我有一个包含3列的网格行:文本(带有填充缩进),虚线和图标。 I expect the result like this: 我期望这样的结果:

结果

The problem is I can't fix draw line width between text and icon and it isn't dash line, as below: 问题是我无法固定文本和图标之间的绘制线宽度,并且它不是虚线,如下所示: 错误

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. 更新 :@Ivan CrojachKaračić:感谢您的帮助。 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). 但是问题是当我将第一列宽度设置为auto ,长文本不能换行并且图标消失(不设置auto ,文本可以正常换行)。 You can see the row "Lennin House,..." from this image: 您可以从此图像中看到“ Lennin House,...”行:

Ë

Can you fix it? 你能修好它吗? Thanks in advance. 提前致谢。

Update 2 : Here is my try with wrap text issue 更新2 :这是我尝试换行问题

<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>

As far as I can see your column definitino is the cause for the problem. 据我所知,您的列definitino是造成此问题的原因。 You allocate 6/10 for the text 3/10 for the line and 1/10 for the icon. 您为行的文本3/10分配6/10 ,为图标1/10分配。

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 将texblock的水平对齐方式设置为strech,一切都应该包裹得很好

Edit: Ah... now I get it...sorry about that. 编辑:啊...现在我明白了...对此感到抱歉。 Just add a MaxWidth to the first column definition (see above) 只需将MaxWidth添加到第一列定义(请参见上文)

you can have dashed lines using "StrokeDashArray" propert. 您可以使用“ StrokeDashArray”属性来设置虚线。 as bellow. 如波纹管。 here 2 is filled area and 1 is the gap. 这里2是填充区域,1是间隙。

<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" . 您甚至可以给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. 这样就再次按1,0.3,4,0.8的比例给出了不均匀的间距,依次为1,0.3,4.0.8,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM