C# WPF - Material Design 文本框的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53468679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 14:11:01  来源:igfitidea点击:

C# WPF - Problem with Material Design Textbox

c#wpfmaterial-design

提问by Coding_Noob

I'm new to programming and started to make an application for school. For that I use C# WPF and for a good-looking Application i installed Material Design.

我是编程新手,开始申请学校。为此,我使用 C# WPF 并且为了好看的应用程序,我安装了 Material Design。

First i started the Demo of Material Design, where you can see what things you can use etc. with the good to copy. So after a quick view I found a nice TextBox with a border and a Checkbox on top of it. The code to copy was this

首先,我开始了 Material Design 的 Demo,在那里你可以看到你可以使用什么东西等等,并且好复制。所以在快速浏览之后,我发现了一个漂亮的 TextBox,上面有一个边框和一个 Checkbox。要复制的代码是这个

<StackPanel>
    <CheckBox x:Name="MaterialDesignOutlinedTextFieldTextBoxEnabledComboBox"
              Margin="32,0,0,8" IsChecked="True">
        Enabled
    </CheckBox>

    <TextBox Height="100" VerticalAlignment="Top" Margin="32,0,0,0"
             TextWrapping="Wrap" AcceptsReturn="True"
             VerticalScrollBarVisibility="Auto"
             Style="{StaticResource MaterialDesignOutlinedTextFieldTextBox}"
             materialDesign:HintAssist.Hint="This is a text area"
             IsEnabled="{Binding Path=IsChecked, ElementName=MaterialDesignOutlinedTextFieldTextBoxEnabledComboBox}" />
</StackPanel>

Because I just needed the Textbox, I deleted the Code of the CheckBox and implemented the rest inside my application. So the code now is like this:

因为我只需要 Textbox,所以我删除了 CheckBox 的代码并在我的应用程序中实现了其余部分。所以现在的代码是这样的:

<TextBox Height="100" Margin="32,0,0,0" VerticalAlignment="Top"
    TextWrapping="Wrap" AcceptsReturn="True"
    VerticalScrollBarVisibility="Auto"
    Style="{StaticResource MaterialDesignOutlinedTextFieldTextBox}"
    materialDesign:HintAssist.Hint="This is a text area" />

The problem now is, that I can't edit the textbox. I guess the TextBox is linked to the CheckBox and because I deleted it, somehow i can't write in it anymore. It all works fine with a standard textbox, but with the copied code i cant do anything, just see the TextBox with its rounded border.

现在的问题是,我无法编辑文本框。我猜 TextBox 与 CheckBox 相关联,因为我删除了它,不知何故我不能再写进去了。使用标准文本框一切正常,但使用复制的代码我无法做任何事情,只能看到带有圆形边框的文本框。

So can anyone help me, how I can implement the textbox with this border and so on AND while I can edit it? I don't know any solution for that right now, so I would appreciate it, if somebody could help me there.

那么任何人都可以帮助我,如何在我可以编辑它的同时实现带有此边框的文本框等等?我现在不知道任何解决方案,所以如果有人可以帮助我,我将不胜感激。

Thanks for your help.

谢谢你的帮助。

回答by Jan Paolo Go

Try following their Super Quick Startguide.

尝试遵循他们的超级快速入门指南。

Below is a modified version of it that uses a TextBox:

下面是它的修改版本,它使用了TextBox

  • Start new WPF project

  • Install MaterialDesignThemesnuget: Install-Package MaterialDesignThemes

  • Edit App.xaml to following:

    <Application ...>
        <Application.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>
        </Application.Resources>
    </Application>
    
  • Edit MainWindow.xaml to following:

    <Window
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        ...>
        <StackPanel>
            <TextBox
                materialDesign:HintAssist.Hint="Hey, that's pretty good!"
                Style="{StaticResource MaterialDesignOutlinedTextFieldTextBox}" />
        </StackPanel>
    </Window>
    
  • 启动新的 WPF 项目

  • 安装MaterialDesignThemesnuget:Install-Package MaterialDesignThemes

  • 将 App.xaml 编辑为以下内容:

    <Application ...>
        <Application.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>
        </Application.Resources>
    </Application>
    
  • 将 MainWindow.xaml 编辑为以下内容:

    <Window
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        ...>
        <StackPanel>
            <TextBox
                materialDesign:HintAssist.Hint="Hey, that's pretty good!"
                Style="{StaticResource MaterialDesignOutlinedTextFieldTextBox}" />
        </StackPanel>
    </Window>
    

回答by domrentable

Remove height from xaml code. This would be an issue.

从 xaml 代码中删除高度。这将是一个问题。