MaterialDesignInXAML TextBox字体颜色为白色(背景色)

时间:2018-08-24 15:20:44

标签: wpf xaml

我有以下代码:

<Window x:Class="App.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:App"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    TextElement.Foreground="{DynamicResource MaterialDesignBody}"
    mc:Ignorable="d"
    Background="{DynamicResource MaterialDesignPaper}"
    FontFamily="{DynamicResource MaterialDesignFont}"
    Title="e-Sura" Height="376.316" Width="525">
<Grid Margin="0,0,0,-7">
    <Grid.RowDefinitions>
        <RowDefinition Height="120"></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <materialDesign:Card Padding="32" Margin="16" Grid.Row="0">
        <TextBlock TextAlignment="Center" Style="{DynamicResource MaterialDesignTitleTextBlock}">e-Sura Login</TextBlock>
    </materialDesign:Card>
    <materialDesign:Card Padding="12" Margin="16" Grid.Row="1" Height="180">
        <Grid>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1.2*"></ColumnDefinition>
                <ColumnDefinition Width="3*"></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>

            <TextBlock Style="{DynamicResource MaterialDesignTitleTextBlock}" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" >User Name :</TextBlock>
            <TextBox Grid.Row="0" Grid.Column="1" Name="UserName" Padding="8,8,8,8" Margin="8,8,8,8" Text="KLPD"></TextBox>

            <TextBlock Style="{DynamicResource MaterialDesignTitleTextBlock}" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right">Password :</TextBlock>
            <PasswordBox Grid.Row="1" Grid.Column="1" Name="Password" Padding="8,8,8,8" Margin="8,8,8,8" Foreground="Black"></PasswordBox>

            <StackPanel Grid.Column="1" Grid.Row="2" Orientation="Horizontal">
                <Button  Padding="8,8,8,8" Margin="8,8,8,8" Width="100">Login</Button>
                <Button  Padding="8,8,8,8" Margin="8,8,8,8" Background="#FFE2D122" BorderBrush="#FFE6D52E" Width="100">Cancel</Button>
            </StackPanel>


        </Grid>
    </materialDesign:Card>
</Grid>

运行时名为“ UserName”的文本框始终具有与控件背景相似​​的字体颜色-即我为白色。 我什至尝试在特定控件上设置ForeColor =“ Black”,但是它不起作用。

我在这里做什么错了?

1 个答案:

答案 0 :(得分:0)

我认为您没有正确使用样式。在您发布的代码中,我无法看到textboxpasswordbox成为您在这些控件上添加的PaddingMargin的原因。

检查以下代码,这些代码运行正常,没有任何问题:

<Window
        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:WpfApplicationTest"
        x:Class="WpfApplicationTest.MainWindow"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        x:Name="win"
        WindowStartupLocation="CenterOwner"
        TextElement.Foreground="{DynamicResource MaterialDesignBody}"
        mc:Ignorable="d"
        Background="{DynamicResource MaterialDesignPaper}"
        FontFamily="{DynamicResource MaterialDesignFont}"
        Title="e-Sura" Height="376.316" Width="525" FontSize="16">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid Margin="0,0,0,-7">
        <Grid.RowDefinitions>
            <RowDefinition Height="120"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <materialDesign:Card Padding="32" Margin="16" Grid.Row="0">
            <TextBlock TextAlignment="Center" Style="{DynamicResource MaterialDesignTitleTextBlock}">e-Sura Login</TextBlock>
        </materialDesign:Card>
        <materialDesign:Card Padding="12" Margin="16" Grid.Row="1" Height="180">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1.2*"></ColumnDefinition>
                    <ColumnDefinition Width="3*"></ColumnDefinition>
                </Grid.ColumnDefinitions>

                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="*"></RowDefinition>
                </Grid.RowDefinitions>

                <TextBlock Grid.Row="0" Grid.Column="0" Margin="0, 10"
                           HorizontalAlignment="Right" Text="User Name :"/>
                <TextBox Grid.Row="0" Grid.Column="1" Name="UserName" Text="KLPD"
                         Margin="8, 0" FontSize="20"
                         Style="{DynamicResource MaterialDesignTextBox}"/>

                <TextBlock Grid.Row="1" Grid.Column="0" Margin="0, 10"
                           HorizontalAlignment="Right" Text="Password :"/>
                <PasswordBox Grid.Row="1" Grid.Column="1"
                             Style="{DynamicResource MaterialDesignPasswordBox}"
                             Margin="8, 0" FontSize="20"
                             Name="Password" />

                <StackPanel Grid.Column="1" Grid.Row="2" Orientation="Horizontal">
                    <Button  Padding="8" Margin="8" Width="100">Login</Button>
                    <Button  Padding="8" Margin="8" Background="#FFE2D122" BorderBrush="#FFE6D52E" Width="100">Cancel</Button>
                </StackPanel>


            </Grid>
        </materialDesign:Card>
    </Grid>
</Window>