我的水印风格阻止了我的文本框信息

时间:2019-03-08 09:56:05

标签: c# sql wpf

我不确定该如何描述。

问题:我正在为app.xaml中的WPF文本框运行样式

<Grid>
   <TextBox Style="{StaticResource WatermarkTextbox}" x:Name="Email" Height="30" Width="300" FontSize="20" FontFamily="Comic Sans MS" Margin="0 40 0 0" />
</Grid>

当我的文本框中有样式时,我无法将书面信息输入到SQL中。这是我在App.xaml中的风格:

<local:BoolToVisibilityConverter x:Key="InputToVisibility"/>
<Style x:Key="WatermarkTextbox" TargetType="{x:Type TextBox}">
    <Setter Property="BorderBrush" Value="Black"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
             <ControlTemplate TargetType="{x:Type TextBox}">
                  <Border Background="{TemplateBinding Background}"
                          BorderBrush="{TemplateBinding BorderBrush}"
                          BorderThickness="{TemplateBinding BorderThickness}">
                      <Grid>
                          <TextBlock Text="Email...">
                              <TextBlock.Visibility>
                                  <MultiBinding Converter="{StaticResource InputToVisibility}">
                                        <Binding ElementName="Email" Path="Text.IsEmpty"/>
                                        <Binding ElementName="Email" Path="IsFocused"/>
                                    </MultiBinding>
                                </TextBlock.Visibility>
                            </TextBlock>
                            <TextBox x:Name="Email" Background="Transparent"/>
                        </Grid>
                    </Border>
             </ControlTemplate>
         </Setter.Value>
    </Setter>
</Style>

花了一些时间才能找到这个问题。现在我完全没有想法。

我的密码和登录屏幕也一样。但是,当我运行代码以查找凭据时,却没有任何回报。如果我删除了样式,它将为我解决所有问题。有人知道我可以保留Waterstyle并使其同时工作吗?

C#登录按钮:

private void Button_Click(object sender, RoutedEventArgs e) // Login
    {
       SqlConnection sqlCon = new SqlConnection(@"Data Source=****;Initial Catalog=LoginDB;Integrated Security=True");

        try
        {
            if (sqlCon.State == System.Data.ConnectionState.Closed)
                sqlCon.Open();

            string query = "SELECT COUNT(1) FROM tblUser WHERE Username=@UserName AND Password=@Password";
            SqlCommand sqlCmd = new SqlCommand(query, sqlCon);
            sqlCmd.CommandType = System.Data.CommandType.Text;

            sqlCmd.Parameters.AddWithValue("@UserName", Email.Text);
            sqlCmd.Parameters.AddWithValue("@Password", Pass.Text);
            int count = Convert.ToInt32(sqlCmd.ExecuteScalar());

            if (count == 1)
            {
                Main.Content = new Page1();
                Framep.Visibility = Visibility.Visible;
                GridS.Visibility = Visibility.Hidden;
            }
            else
            {
                MessageBox.Show("Username or password is incorrect");
            }

        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message, "App Information");
        }
        finally
        {
            sqlCon.Close();
        } 
    }
}

SQL:

go
USE [LoginDB]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[tblUser](
    [UserID] [int] NULL,
    [UserName] [nvarchar](50) NULL,
    [Password] [nvarchar](50) NULL
) ON [PRIMARY]

GO

1 个答案:

答案 0 :(得分:0)

在这里!

我稍微修改了您的风格,以使您对应该做什么有所了解。 我会说https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/textbox-styles-and-templates仍然是正确执行此操作的方法!

<Style x:Key="WatermarkTextbox" TargetType="{x:Type TextBox}">
                <Setter Property="BorderBrush" Value="Black"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TextBox}">
                            <Border Background="{TemplateBinding Background}"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}">
                                <Grid>
                                    <ScrollViewer x:Name="PART_ContentHost" />
                                    <TextBlock x:Name="TxBlkEmail" Text="Email..." Visibility="Collapsed">
                                    </TextBlock>
                                </Grid>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="Text" Value="{x:Null}">
                                    <Trigger.Setters>
                                        <Setter TargetName="TxBlkEmail" Property="Visibility" Value="Visible"/>
                                    </Trigger.Setters>
                                </Trigger>
                                <Trigger Property="Text" Value="">
                                    <Trigger.Setters>
                                        <Setter TargetName="TxBlkEmail" Property="Visibility" Value="Visible"/>
                                    </Trigger.Setters>
                                </Trigger>

                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>