网格可见性状态更改后,文本框为空

时间:2014-02-04 15:03:16

标签: wpf vb.net textbox grid

遇到一些问题并且没有在互联网上遇到答案或任何类似情况:

我在wpf应用程序的主窗体上有四个按钮。每个按钮控制窗体上四个网格的可见性。以下是每个按钮的点击事件的示例代码:

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As
                     System.Windows.RoutedEventArgs) Handles Button.Click

    grid1.Visibility = Windows.Visibility.Hidden
    grid2.Visibility = Windows.Visibility.Hidden
    grid3.Visibility = Windows.Visibility.Hidden
    grid4.Visibility = Windows.Visibility.Visible
    textbox1.Focus()

End Sub

只有一个按钮有任何其他代码,以下是它的一个示例:

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As
              System.Windows.RoutedEventArgs) Handles Button.Click

    grid1.Visibility = Windows.Visibility.Hidden
    grid2.Visibility = Windows.Visibility.Hidden
    grid3.Visibility = Windows.Visibility.Hidden
    grid4.Visibility = Windows.Visibility.Visible
    Textbox.Focus()

    If Textbox1.Text = vbNullString Then
        dblTotal = 0
    Else
        dblTotal = CDbl(Textbox1.Text)
    End If

    If dblTotal > 5 Then
        StackPanel.Visibility = Windows.Visibility.Visible
        dblOverQts = dblTotal - 5
        Textbox2.Text = CStr(dblOverQts)
        dblPrice = dblPrice * dblOverQts
        txtPrice.Text = FormatCurrency(CStr(dblPrice))
        lblInvQtsOver.Content = CStr(dblOverQts)
        lblInvPrice.Content = FormatCurrency(CStr(dblPrice))
        lblInvPrice.Visibility = Windows.Visibility.Visible
        lblInvQtsOver.Visibility = Windows.Visibility.Visible
    End If

    totalChanger()

End Sub

以上代码与可见性状态发生变化后空白的文本框无关。

Grid1和Grid2都有文本框,在继续下一个网格之前由用户填写。我可以在网格之间切换一次,但如果我尝试不止一次,则文本框在第二次可见性更改时变为空。我需要用户能够根据需要在网格之间切换,以对信息进行任何必要的更改。我完全没有错误,也找不到任何导致这种情况发生的代码。

以下是丢失文本框值的主网格的xaml:

<Grid x:Name="grdVehInfo" Margin="247,28,12,31" Background="#00F2A1A1" UseLayoutRounding="False" Panel.ZIndex="0" Grid.RowSpan="2" Visibility="Hidden">
            <Label Content="Vehicle Id : " Height="28" HorizontalAlignment="Left" Margin="40,30,0,0" x:Name="lblVehID" VerticalAlignment="Top" Width="77" FontWeight="Bold" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="115,30,0,0" x:Name="txtVehID" VerticalAlignment="Top" Width="136" CharacterCasing="Upper" BorderBrush="{x:Null}" TabIndex="8" ToolTip="Last 8 digits of Vin#" FontWeight="Bold" Background="#00000000">
                <TextBox.BindingGroup>
                    <BindingGroup/>
                </TextBox.BindingGroup>
            </TextBox>
            <Label Content="Licence Plate : " Height="28" HorizontalAlignment="Left" Margin="268,30,0,0" x:Name="Label1" VerticalAlignment="Top" Width="92" FontWeight="Bold" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="366,30,0,0" x:Name="txtLicencePlate" VerticalAlignment="Top" Width="120" CharacterCasing="Upper" Background="{x:Null}" BorderBrush="{x:Null}" TabIndex="9" ToolTip="License Plate #" FontWeight="Bold" />
            <Label  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:s="clr-namespace:System;assembly=mscorlib" 
                Height="28" 
                HorizontalAlignment="Left" 
                Margin="544,30,0,0" 
                x:Name="lblDate" 
                VerticalAlignment="Top" 
                Width="137" 
                Content="{x:Static s:DateTime.Now}" 
                DataContext="{Binding Mode=OneWay, StringFormat=\{0:d\}}" FontWeight="Bold" Target="{Binding}" />
            <Label Content="Make : " Height="28" HorizontalAlignment="Left" Margin="40.007,81,0,0" x:Name="lblMake" VerticalAlignment="Top" FontWeight="Bold" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="97.024,81,0,0" x:Name="txtMake" VerticalAlignment="Top" Width="145.976" Background="{x:Null}" BorderBrush="{x:Null}" TabIndex="10" FontWeight="Bold" />
            <Label Content="Model : " Height="28" HorizontalAlignment="Left" Margin="268,81,0,0" x:Name="lblModel" VerticalAlignment="Top" FontWeight="Bold" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="330.027,83,0,0" x:Name="txtModel" VerticalAlignment="Top" Width="155.973" Background="{x:Null}" BorderBrush="{x:Null}" TabIndex="11" FontWeight="Bold" />
            <Label Content="Year : " Height="28" HorizontalAlignment="Left" Margin="544,81,0,0" x:Name="lblModelYear" VerticalAlignment="Top" FontWeight="Bold" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="590,81,0,0" x:Name="txtModelYear" VerticalAlignment="Top" Width="80" Background="{x:Null}" BorderBrush="{x:Null}" TabIndex="12" FontWeight="Bold" />
            <Label Content="Engine : " Height="28" HorizontalAlignment="Left" Margin="40.007,138,0,0" x:Name="lblEngine" VerticalAlignment="Top" FontWeight="Bold" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="104.12,138,0,0" x:Name="txtEngine" VerticalAlignment="Top" Width="71" Background="{x:Null}" BorderBrush="{x:Null}" TabIndex="13" FontWeight="Bold" />
            <Label Content="Mileage : " Height="28" HorizontalAlignment="Left" Margin="213.007,138,0,0" x:Name="lblMileage" VerticalAlignment="Top" FontWeight="Bold" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="284.057,138,0,0" x:Name="txtMileage" VerticalAlignment="Top" Width="120" Background="{x:Null}" BorderBrush="{x:Null}" TabIndex="14" FontWeight="Bold" />
            <Label Content="Oil Filter : " Height="28" HorizontalAlignment="Left" Margin="429.07,138,0,0" x:Name="lblOilFilter" VerticalAlignment="Top" FontWeight="Bold" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="503.173,138,0,0" x:Name="txtOilFilter" VerticalAlignment="Top" Width="88.827" Background="{x:Null}" BorderBrush="{x:Null}" TabIndex="15" FontWeight="Bold" />
            <Label Content="Qts : " Height="28" HorizontalAlignment="Left" Margin="598,138,0,0" x:Name="lblQts" VerticalAlignment="Top" FontWeight="Bold" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="642.923,138,0,0" x:Name="txtQts" VerticalAlignment="Top" Width="53" Background="{x:Null}" BorderBrush="{x:Null}" TabIndex="16" FontWeight="Bold" />
            <Label Content="Brand Oil : " Height="28" HorizontalAlignment="Left" Margin="40,192,0,0" x:Name="lblBrandOil" VerticalAlignment="Top" FontWeight="Bold" />
            <ComboBox Height="23" HorizontalAlignment="Left" Margin="115,192,0,0" x:Name="cbxBrandOil" VerticalAlignment="Top" Width="147" Background="{x:Null}" BorderBrush="{x:Null}" TabIndex="17" FontWeight="Bold" />
            <Label Content="Oil Wieght : " Height="28" HorizontalAlignment="Left" Margin="298,192,0,0" x:Name="lblOilWieght" VerticalAlignment="Top" FontWeight="Bold" />
            <ComboBox Height="23" HorizontalAlignment="Left" Margin="384.39,192,0,0" x:Name="cbxWeightOil" VerticalAlignment="Top" Width="112.783" Background="{x:Null}" BorderBrush="{x:Null}" TabIndex="18" FontWeight="Bold" />
            <Label Content="Hood Tech : " Height="28" HorizontalAlignment="Left" Margin="40.007,248,0,0" x:Name="lblHoodTech" VerticalAlignment="Top" FontWeight="Bold" />
            <ComboBox Height="23" HorizontalAlignment="Left" Margin="123,247,0,0" x:Name="cbxHoodTech" VerticalAlignment="Top" Width="62" Background="{x:Null}" BorderBrush="{x:Null}" TabIndex="19" FontWeight="Bold" />
            <Label Content="Pit Tech : " Height="28" HorizontalAlignment="Left" Margin="224.007,248,0,0" x:Name="lblPitTech" VerticalAlignment="Top" FontWeight="Bold" />
            <ComboBox Height="23" HorizontalAlignment="Left" Margin="300,247,0,0" x:Name="cbxPitTech" VerticalAlignment="Top" Width="62" Background="{x:Null}" BorderBrush="{x:Null}" TabIndex="20" FontWeight="Bold" />
            <Label Content="Comments : " Height="28" HorizontalAlignment="Left" Margin="40.007,300,0,0" x:Name="lblComment" VerticalAlignment="Top" FontWeight="Bold" />
            <TextBox Height="127" HorizontalAlignment="Left" Margin="125,300,0,0" x:Name="txtComment" VerticalAlignment="Top" Width="253" Background="{x:Null}" BorderBrush="{x:Null}" TabIndex="21" FontWeight="Bold" />
            <Border BorderBrush="#FF8D8787" BorderThickness="0" Height="690" HorizontalAlignment="Left" Margin="5,0,0,0" x:Name="brdVehicleInfo" VerticalAlignment="Top" Width="718" CornerRadius="15" Panel.ZIndex="-1">
                <Border.Background>
                    <SolidColorBrush Color="#FFB9B7B7" Opacity="0.3"/>
                </Border.Background>
                <Border.Effect >
                    <DropShadowEffect BlurRadius="10" ShadowDepth="10"/>
                </Border.Effect>
                <DataGrid x:Name="dgPastServices" Margin="40,457,26,23" HeadersVisibility="Column" CanUserReorderColumns="False" AutoGenerateColumns="False" SelectedIndex=" -1">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header= "Service Date" Binding= "{Binding Path=ServiceDate}"/>
                        <DataGridTextColumn Header= "Mileage" Binding= "{Binding Path=Mileage}"/>
                        <DataGridTextColumn Header="License Plate" Binding= "{Binding Path= LicensePlate}"/>
                        <DataGridTextColumn Header="Oil Brand" Binding= "{Binding Path= OilBrand}"/>
                        <DataGridTextColumn Header="Oil Weight" Binding= "{Binding Path= OilWeight}"/>
                        <DataGridTextColumn Header="Comments" Binding= "{Binding Path= Comments}"/>
                    </DataGrid.Columns> 
                    <DataGrid.BindingGroup>
                        <BindingGroup/>
                    </DataGrid.BindingGroup>
                </DataGrid>
            </Border>
        </Grid>

以下是按钮的xaml:

<Button BorderBrush="#6928A3A3" BorderThickness="0" Content="Vehicle Info" FontWeight="Bold" Height="57" HorizontalAlignment="Left" Margin="32,140,0,0" x:Name="btnVehicleInfo" OpacityMask="{x:Null}" Style="{DynamicResource RoundedButton}" VerticalAlignment="Top" Width="168">
            <Button.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF0E4E76" Offset="0" />
                    <GradientStop Color="#FFA4CEE8" Offset="0.439" />
                </LinearGradientBrush>
            </Button.Background>
            <Button.Effect>
                <DropShadowEffect BlurRadius="10" ShadowDepth="5"/>
            </Button.Effect>
        </Button>

0 个答案:

没有答案