加载数据会扭曲表单设计

时间:2015-03-12 07:04:55

标签: wpf vb.net

我在wpf中有一个窗口,看起来像没有代码隐藏: Image with no code-behind

既然我的表单看起来像我想要的那样,我使用以下内容从我的SQL Server数据库中获取数据并将其加载到表单中:

Private Sub winVehicleExpenses_Loaded(sender As Object, e As RoutedEventArgs) Handles winVehicleExpenses.Loaded
    taVehicleExpenses = New PIMDataSetTableAdapters.taVehicleExpenses
    taVehicleExpenses.Fill(dsPIM.VehicleExpenses)   'Load all the Expense data into the PIM dataset
    Dim dvTypes As DataView = New DataView(dsPIM.Tables("StandardEntries"), "CategoryID = 13", "Entry", DataViewRowState.CurrentRows)   'CategoryID = 13 are the Vehicle Expense Types
    With cboTypes
        .ItemsSource = dvTypes
        .SelectedIndex = 0  'Move to the first entry
    End With
End Sub

现在,当我运行应用程序时,表单如下所示: Window when loading data

注意" Notes"之间的差距。标签和加载数据之前不存在的备注文本框。

如何将数据加载到列表框和一些文本框的简单过程如何更改表单布局?

ADDED:这是XAML

<Window
x:Class="VehicleExpenses"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="VehicleExpense"
xmlns:local="clr-namespace:PIM"
x:Name="winVehicleExpenses"
Height="490"
Width="440"
ShowInTaskbar="False"
Background="#FFE8FFFD"
IsTabStop="False">
<Window.Resources>
    <local:DisplayDateFormatter x:Key="FormatDisplayDate" />
    <local:DisplayCurrencyFormatter x:Key="FormatCurrency" />
    <local:DisplayFixedFormatter x:Key="FormatSingle" />
    <Style x:Key="ItemHeaders" TargetType="Label">
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="Grid.Row" Value="1" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="VerticalAlignment" Value="Bottom" />
    </Style>
    <Style x:Key="ItemLabels" TargetType="Label">
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="HorizontalContentAlignment" Value="Right" />
        <Setter Property="Height" Value="26" />
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Width" Value="80" />
        <Setter Property="Margin" Value="0,5,0,0" />
    </Style>
    <Style x:Key="PreviousTextBoxes" TargetType="TextBox">
        <Setter Property="Height" Value="26" />
        <Setter Property="Margin" Value="0,5,0,0" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="Width" Value="80" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Background" Value="Cornsilk" />
    </Style>
    <Style TargetType="Button">
        <Setter Property="Height" Value="26" />
        <Setter Property="Width" Value="60" />
        <Setter Property="Margin" Value="60,0,0,0" />
        <Setter Property="FontWeight" Value="Bold" />
    </Style>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="40" />
        <RowDefinition Height="30" />
        <RowDefinition Height="225" />
        <RowDefinition Height="100" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="90"/>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <StackPanel
        Grid.Column="0"
        Grid.Row="0"
        Grid.ColumnSpan="3"
        Orientation="Horizontal"
        HorizontalAlignment="Center">
        <Label
            Height="26"
            FontWeight="Bold"
            Content="Expense Type:" />
        <ComboBox
            Name="cboTypes"
            Height="26"
            Width="120"
            FontSize="13"
            DisplayMemberPath="Entry"
            SelectedValuePath="EntryID"
            Background="White" />
    </StackPanel>
    <Label
        Grid.Column="0"
        Content="Item"
        Width="80"
        Style="{StaticResource ItemHeaders}" />
    <StackPanel
        Grid.Column="0"
        Grid.Row="2"
        Height="220"
        VerticalAlignment="Top">
        <Label
            Name="lblChargeDate"
            Content="Charge Date:"
            Style="{StaticResource ItemLabels}" />
        <Label
            Name="lblMileage"
            Content="Mileage:"
            Style="{StaticResource ItemLabels}" />
        <Label
            Name="lblGallons"
            Content="Gallons:"
            Style="{StaticResource ItemLabels}" />
        <Label
            Name="lblCharge"
            Content="Charge:"
            Style="{StaticResource ItemLabels}" />
        <Label
            Name="lblStartDate"
            Content="Start Date:"
            Style="{StaticResource ItemLabels}" />
        <Label
            Name="lblEndDate"
            Content="End Date:"
            Style="{StaticResource ItemLabels}" />
    </StackPanel>
    <Label
        Content="Previous"
        Grid.Column="1"
        Width="93" 
        Style="{StaticResource ItemHeaders}" />
    <Label
        Content="Current"
        Grid.Column="2"
        Width="93"
        Style="{StaticResource ItemHeaders}" />
    <StackPanel
        Name="pnlPrevious"
        Grid.Column="1"
        Grid.Row="2"
        Height="220"
        VerticalAlignment="Top">
        <TextBox
            Text="{Binding Path=ChargeDate, Converter={StaticResource FormatDisplayDate}}"
            Style="{StaticResource PreviousTextBoxes}"
            Focusable="False" />
        <TextBox
            Name="txtPreviousMileage"
            Text="{Binding Path=Mileage}"
            Style="{StaticResource PreviousTextBoxes}"
            Focusable="False" />
        <TextBox
            Name="txtPreviousGallons"
            Text="{Binding Path=Gallons, Converter={StaticResource FormatSingle}, ConverterParameter=3}"
            Style="{StaticResource PreviousTextBoxes}"
            Focusable="False" />
        <TextBox
            Text="{Binding Path=Charge, Converter={StaticResource FormatCurrency}}"
            Style="{StaticResource PreviousTextBoxes}"
            Focusable="False" />
        <TextBox
            Name="txtPreviousStartDate"
            Text="{Binding Path=StartDate, Converter={StaticResource FormatDisplayDate}}"
           Style="{StaticResource PreviousTextBoxes}"
            Focusable="False" />
        <TextBox
            Name="txtPreviousEndDate"
            Text="{Binding Path=EndDate, Converter={StaticResource FormatDisplayDate}}"
            Style="{StaticResource PreviousTextBoxes}"
            Focusable="False" />
        <Label
            Name="lblNotes"
            Content="Notes:"
            HorizontalAlignment="Right"
            VerticalAlignment="Bottom"
            Margin="0,0,30,0"
            Style="{StaticResource ItemLabels}" />
    </StackPanel>
    <StackPanel
        Grid.Column="2"
        Grid.Row="2"
        Height="220"
        VerticalAlignment="Top">
        <DatePicker
            Name="dprCurrentChargeDate"
            HorizontalAlignment="Left"
            Height="23"
            VerticalAlignment="Top"
            Width="120" />
        <TextBox
            Name="txtCurrentMileage"
            HorizontalAlignment="Left"
            Height="23"
            VerticalAlignment="Top"
            Width="120" />
        <TextBox
            Name="txtCurrentsGallons"
            HorizontalAlignment="Left"
            Height="23"
            VerticalAlignment="Top"
            Width="120" />
        <TextBox
            Name="txtCurrentCharge"
            HorizontalAlignment="Left"
            Height="23"
            VerticalAlignment="Top"
            Width="120" />
        <DatePicker
            Name="dprCurrentStartDate"
            HorizontalAlignment="Left"
            Height="23"
            VerticalAlignment="Top"
            Width="120" />
        <DatePicker
            Name="dprCurrentEndDate"
            HorizontalAlignment="Left"
            Height="23"
            VerticalAlignment="Top"
            Width="120" />
    </StackPanel>
    <StackPanel
        Grid.Column="0"
        Grid.ColumnSpan="3"
        Grid.Row="3"
        VerticalAlignment="Stretch"
        HorizontalAlignment="Stretch"
        Orientation="Horizontal">
        <TextBox
            Name="txtPreviousNotes"
            VerticalAlignment="Stretch"
            Width="175"
            Text="{Binding Notes}"
            Background="Cornsilk"
            Focusable="False"
            Margin="10,0,0,0"/>
        <TextBox
            Name="txtCurrentNotes"
            VerticalAlignment="Stretch"
            Width="175"
            Margin="55,0,0,0" />
    </StackPanel>
    <StackPanel
        Grid.Column="0"
        Grid.Row="4"
        Grid.ColumnSpan="3"
        Orientation="Horizontal">
        <Button
            Name="btnCancel"
            Content="Cancel"
            IsTabStop="False" />
        <Button
            Name="btnClose"
            Content="Close"
            IsTabStop="False" />
        <Button
            Name="btnView"
            Content="View"
            IsTabStop="False" />
    </StackPanel>
</Grid>

2 个答案:

答案 0 :(得分:2)

好吧,在您发布代码并进一步解释您正在做什么之后,很明显发生了什么。 Label lblNotes位于pnlPrevious StackPanel,其VerticalAlignment属性设置为Top。将VerticalAlignment中的Label设置为Bottom没有任何区别,因为它是相对属性,这对StackPanel几乎没有影响。因此,当您折叠部分控件时,pnlPrevious StackPanel中的内容会重新排列并占用所需的空间。

将内容与StackPanel的底部对齐而不在其中插入其他面板的唯一方法是将StackPanel本身与Bottom对齐。但是,对于您的布局来说,这将是一个糟糕的决定。

为了避免头痛和沮丧,请将lblNotes移出pnlPrevious并移到自己的行中,在StackPanel包含备注文本框的正上方。

答案 1 :(得分:0)

有许多方法可以在XAML中创建LayOut。 如果没有实际的XAML,那就是对Lay Out如何改变的猜测。