Wpf TextBlock.Inlines-添加水平线

Wpf TextBlock.Inlines-添加水平线,wpf,textblock,Wpf,Textblock,下面我添加了被水平线分隔的日志条目 Dim vLogText = vLog.Split("^") Dim vRows As Integer = vLogText.Length - 1 For i As Integer = 0 To vRows Dim Subrow As String = vLogText(i) LogTB.Inlines.Add(Subrow)

下面我添加了被水平线分隔的日志条目

 Dim vLogText = vLog.Split("^")
            Dim vRows As Integer = vLogText.Length - 1
            For i As Integer = 0 To vRows
                Dim Subrow As String = vLogText(i)
                LogTB.Inlines.Add(Subrow)
                LogTB.Inlines.Add(New Line With {.X1 = 0, .Y1 = 0, .X2 = 300, .Y2 = 0, .Stroke = New SolidColorBrush(Colors.Gray), .StrokeThickness = 4.0})
            Next
如果我想要一个预设的长度(在上面的例子中是300),这是可以的,但是如果它需要延伸到容器的整个宽度,如何实现呢

谢谢

在回复Anjum提供的答案时添加

下面是如何添加网格的

#Region "Right Grid"

Private Function RightGrid() As Grid
    Try
        Dim MainGrid As New Grid
        Dim vGrid As New Grid
        Dim SV As New ScrollViewer
        With SV
            .Name = "RightGrid_SV"
            .Content = vGrid
            .VerticalScrollBarVisibility = ScrollBarVisibility.Auto
        End With
        RegisterControl(WorkOrder_Grid, SV)
        MainGrid.Children.Add(SV)


        'Add in the status and log

        Dim LogLB As New Label
        With LogLB
            .Name = "WorkOrder_LogLB"
        End With
        RegisterControl(WorkOrder_Grid, LogLB)
        vGrid.Children.Add(LogLB)

        If IsNewRecord = True Then
            'Add some help data
            Dim SP As New StackPanel


            Dim HeaderTB As New TextBlock
            With HeaderTB
                .Text = "ADDING A NEW WORK ORDER" & Environment.NewLine
                .HorizontalAlignment = HorizontalAlignment.Center
                .FontWeight = FontWeights.Bold
            End With

            Dim DatesHeaderTB As New TextBlock
            With DatesHeaderTB
                .Text = "Dates"
                .TextDecorations = TextDecorations.Underline
            End With

            Dim DatesContentTB As New TextBlock
            With DatesContentTB
                .Text = "Enter the Work Order date and the date the Work needs to be completed by." & Environment.NewLine
                .TextWrapping = TextWrapping.Wrap
            End With

            Dim UnitHeaderTB As New TextBlock
            With UnitHeaderTB
                .Text = "Unit/Common Area"
                .TextDecorations = TextDecorations.Underline
            End With

            Dim vUnit As String = "If the Work Order relates to a homeowners property, insert the details using the button. "
            vUnit += "If the homeowners have a registered account they will be updated by email each time the Work Order status is changed!" & Environment.NewLine & Environment.NewLine
            vUnit += "If the Work Order relates to a common area (e.g. Recreation grounds, Clubhouse...) just enter a short description of that area." & Environment.NewLine
            Dim UnitContentTB As New TextBlock
            With UnitContentTB
                .Text = vUnit
                .TextWrapping = TextWrapping.Wrap
            End With

            Dim TypeHeaderTB As New TextBlock
            With TypeHeaderTB
                .Text = "Work Type"
                .TextDecorations = TextDecorations.Underline
            End With

            Dim TypeContentTB As New TextBlock
            With TypeContentTB
                .Text = "A short description of the type of work (e.g. Spinklers, Lights not working...)" & Environment.NewLine
                .TextWrapping = TextWrapping.Wrap
            End With

            Dim DetailsHeaderTB As New TextBlock
            With DetailsHeaderTB
                .Text = "Details/Instructions"
                .TextDecorations = TextDecorations.Underline
            End With

            Dim DetailsContentTB As New TextBlock
            With DetailsContentTB
                .Text = "Add any more details or instructions to help the supplier." & Environment.NewLine
                .TextWrapping = TextWrapping.Wrap
            End With

            Dim SupplierHeaderTB As New TextBlock
            With SupplierHeaderTB
                .Text = "Supplier"
                .TextDecorations = TextDecorations.Underline
            End With

            Dim SupplierContentTB As New TextBlock
            With SupplierContentTB
                .Text = "Insert the supplier using the button." & Environment.NewLine & Environment.NewLine & "You can still save the Work Order without entering a supplier, but the Work Order document will not be generated!" & Environment.NewLine
                .TextWrapping = TextWrapping.Wrap
            End With

            Dim EmailHeaderTB As New TextBlock
            With EmailHeaderTB
                .Text = "Supplier Email"
                .TextDecorations = TextDecorations.Underline
            End With

            Dim EmailContentTB As New TextBlock
            With EmailContentTB
                .Text = "The default email address will be loaded when you insert the supplier (this can be overridden) " & Environment.NewLine & Environment.NewLine & "If the email address is blank, or not valid, the Work Order document will not be emailed, but generated as a PDF to print locally and mail!" & Environment.NewLine
                .TextWrapping = TextWrapping.Wrap
            End With

            Dim ImageHeaderTB As New TextBlock
            With ImageHeaderTB
                .Text = "Upload Image"
                .TextDecorations = TextDecorations.Underline
            End With

            Dim ImageContentTB As New TextBlock
            With ImageContentTB
                .Text = "If you have a photograph of the work required, browse to the image. It will be included in the Work Order document." & Environment.NewLine
                .TextWrapping = TextWrapping.Wrap
            End With

            Dim CostHeaderTB As New TextBlock
            With CostHeaderTB
                .Text = "Estimated Cost"
                .TextDecorations = TextDecorations.Underline
            End With

            Dim vCost As String = "If you enter an estimated cost the Work Order will be authorised up to that amount" & Environment.NewLine & Environment.NewLine
            vCost += "If the supplier is unable to carry out the work for this amount or less (or the field is left at zero) the work will not be authorised and they must revert back to the sender with a quote!" & Environment.NewLine & Environment.NewLine
            vCost += "When estimated costs are used a lot it is a good idea to check the supplier invoices do not regularly equal (or a dollar or two less) than the estimated amount!!" & Environment.NewLine

            Dim CostContentTB As New TextBlock
            With CostContentTB
                .Text = vCost
                .TextWrapping = TextWrapping.Wrap
            End With

            Dim SiteHeaderTB As New TextBlock
            With SiteHeaderTB
                .Text = "Smart Manager"
                .TextDecorations = TextDecorations.Underline
                .Foreground = New SolidColorBrush(Colors.Blue)
            End With

            Dim SiteContentTB As New TextBlock
            With SiteContentTB
                .Text = "You can also enter Work Orders, whilst on-site, with a Smart Phone or Tablet using Smart Manager!" & Environment.NewLine
                .TextWrapping = TextWrapping.Wrap
                .Foreground = New SolidColorBrush(Colors.Blue)
            End With

            With SP.Children
                .Add(HeaderTB)
                .Add(DatesHeaderTB)
                .Add(DatesContentTB)
                .Add(UnitHeaderTB)
                .Add(UnitContentTB)
                .Add(TypeHeaderTB)
                .Add(TypeContentTB)
                .Add(DetailsHeaderTB)
                .Add(DetailsContentTB)
                .Add(SupplierHeaderTB)
                .Add(SupplierContentTB)
                .Add(EmailHeaderTB)
                .Add(EmailContentTB)
                .Add(ImageHeaderTB)
                .Add(ImageContentTB)
                .Add(CostHeaderTB)
                .Add(CostContentTB)
                .Add(SiteHeaderTB)
                .Add(SiteContentTB)
            End With

            LogLB.Content = SP
        End If

        Return MainGrid
    Catch ex As Exception
        EmailError(ex)
        Return Nothing
    End Try
End Function
…这是主电网的一部分

Private Function CentreGrid() As Grid
    Try
        Dim vGrid As New Grid
        For i As Integer = 0 To 2
            Dim vCol As New ColumnDefinition
            If i = 1 Then
                vCol.Width = New GridLength(5, GridUnitType.Auto)
            End If
            vGrid.ColumnDefinitions.Add(vCol)
        Next

        Dim vLeftGrid As Grid = LeftGrid()
        Grid.SetColumn(vLeftGrid, 0)
        vGrid.Children.Add(vLeftGrid)

        Dim vGridSplitter As New GridSplitter
        With vGridSplitter
            .VerticalAlignment = Windows.VerticalAlignment.Stretch
            .HorizontalAlignment = Windows.HorizontalAlignment.Center
            .ResizeBehavior = GridResizeBehavior.PreviousAndNext
            .Background = New SolidColorBrush(Colors.Blue)
            .Width = 5
            .Margin = New Thickness(5)
        End With
        Grid.SetColumn(vGridSplitter, 1)
        vGrid.Children.Add(vGridSplitter)

        Dim vRightGrid As Grid = RightGrid()
        Grid.SetColumn(vRightGrid, 2)
        vGrid.Children.Add(vRightGrid)

        Return vGrid
    Catch ex As Exception
        EmailError(ex)
        Return Nothing
    End Try
End Function
当页面打开时,这是水平线的样子(蓝色)

…这就是GridSplitter向左移动时发生的情况(如果窗口变大,情况也是如此)


我不会为类似的事情使用代码隐藏。绝对痛苦。如果将大多数UI开发限制在XAML前端,您会发现使用
WPF
要容易得多。试试这样的

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication3"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="MyTemplate">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="{Binding}" />
                    <Line X1="0" X2="{Binding ActualWidth, RelativeSource={RelativeSource Self}}" Stroke="Black" />
                </StackPanel>
            </DataTemplate>
        </Grid.Resources>
        <ItemsControl ItemTemplate="{StaticResource MyTemplate}">
            <ItemsControl.Items>
                <sys:String>One</sys:String>
                <sys:String>Two</sys:String>
                <sys:String>Three</sys:String>
            </ItemsControl.Items>
        </ItemsControl>
    </Grid>
</Window>

一
二
三

我不会将代码隐藏用于这样的事情。绝对痛苦。如果将大多数UI开发限制在XAML前端,您会发现使用
WPF
要容易得多。试试这样的

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication3"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="MyTemplate">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="{Binding}" />
                    <Line X1="0" X2="{Binding ActualWidth, RelativeSource={RelativeSource Self}}" Stroke="Black" />
                </StackPanel>
            </DataTemplate>
        </Grid.Resources>
        <ItemsControl ItemTemplate="{StaticResource MyTemplate}">
            <ItemsControl.Items>
                <sys:String>One</sys:String>
                <sys:String>Two</sys:String>
                <sys:String>Three</sys:String>
            </ItemsControl.Items>
        </ItemsControl>
    </Grid>
</Window>

一
二
三

最好为
X2
属性设置
绑定
,并将其设置为
文本块的
实际宽度

下面是C#代码,您可以将其更改为VB.net:

        Line line = new Line() { Stroke = Brushes.Blue};
        Binding binding = new Binding("ActualWidth");
        binding.RelativeSource = new RelativeSource() { Mode=RelativeSourceMode.FindAncestor, AncestorType=typeof(TextBlock) };
        BindingOperations.SetBinding(line, Line.X2Property, binding);

        LogTB.Inlines.Add(line);

最好为
X2
属性设置
绑定
,并将其设置为
TextBlock
ActualWidth

下面是C#代码,您可以将其更改为VB.net:

        Line line = new Line() { Stroke = Brushes.Blue};
        Binding binding = new Binding("ActualWidth");
        binding.RelativeSource = new RelativeSource() { Mode=RelativeSourceMode.FindAncestor, AncestorType=typeof(TextBlock) };
        BindingOperations.SetBinding(line, Line.X2Property, binding);

        LogTB.Inlines.Add(line);


这非常接近-它在加载页面时设置宽度,但如果调整TextBlock大小(窗口大小更改,gridsplitter移动…),它将保持不变width@gchq不,不,我已经检查过了。安茹-谢谢你抽出时间!我刚刚添加了两个屏幕截图来显示之前和之后,以及一些额外的代码来显示它是如何部署的,因为这可能是一个促成因素。找到了它-将祖先类型更改为Grid,现在它工作得很好-谢谢你让我进入正确的轨道,非常接近-它设置了页面加载时的宽度,但是,如果调整TextBlock大小(窗口大小更改,gridsplitter移动…),它将保持不变width@gchq不,不,我已经检查过了。安茹-谢谢你抽出时间!我刚刚添加了几个屏幕截图来显示之前和之后,以及一些额外的代码来显示它是如何部署的,因为这可能是一个促成因素。找到了它-将祖先类型更改为网格,现在它工作得很好-感谢您让我走上正确的轨道Hi Jace-感谢您的回复。该项目太大,无法管理XAML(一页可以有20k行),并且有许多DB-fed动态设置,因此除了主窗口的框架之外,所有内容都是用代码编写的。有了代码隐藏,就更容易定位和进行更改。你有义务参加MVVM和MVC速成班。每当我听到有人说“一切都是用代码编写的”时,他们通常都会以艰难的方式来完成。Jace-A感谢你所说的,这可能要困难得多-但由于动态加载太多,我别无选择。在问题的一部分示例中,根据用户选择将不同的网格加载到页面上,然后根据后端数据动态加载其他项目。我已经标记了这一点,但不是作为答案,因为它基本上是正确的,但不是我要找的格式。您可能希望提交一个示例。老实说,从您描述情况的方式来看,这听起来像是XAML项目的类型。该项目太大,无法管理XAML(一页可以有20k行),并且有许多DB-fed动态设置,因此除了主窗口的框架之外,所有内容都是用代码编写的。有了代码隐藏,就更容易定位和进行更改。你有义务参加MVVM和MVC速成班。每当我听到有人说“一切都是用代码编写的”时,他们通常都会以艰难的方式来完成。Jace-A感谢你所说的,这可能要困难得多-但由于动态加载太多,我别无选择。在问题的一部分示例中,根据用户选择将不同的网格加载到页面上,然后根据后端数据动态加载其他项目。我已经标记了这一点,但不是作为答案,因为它基本上是正确的,但不是我要找的格式。您可能希望提交一个示例。老实说,从您描述情况的方式来看,这听起来像是为XAML项目类型而设计的。