目前,我有一个包含 3 列的网格行:文本(带有填充缩进)、虚线和图标。我期望这样的结果:
问题是我无法修复文本和图标之间的画线宽度,它不是虚线,如下所示:
这是我的数据模板:
<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>
还有我的列表框:
<ListBox x:Name="lstTreeView" HorizontalAlignment="Stretch" VerticalAlignment="Top" ItemsSource="{Binding TreeViewModel.TreeData, Mode=TwoWay}" ItemTemplate="{StaticResource TreeItemTemplate}"/>
有没有办法做到这一点?
更新:@Ivan Crojach Karačić:感谢您的帮助。我试了你的方法,成功了。但问题是当我将第一列宽度设置为时auto
,长文本无法换行并且图标消失(不设置auto
,文本可以正常换行)。您可以从这张图片中看到“Lennin House,...”这一行:
你能修好它吗?提前致谢。
更新 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>