materialdesigninxaml-TextBox验证文本与另一个文本框重叠

时间:2019-07-14 09:46:24

标签: c# wpf xaml material-design-in-xaml

我正在尝试制作简单的登录表单卡,并且内部有用于用户名的TextBox和用于密码的PasswordBox。我已经添加了验证规则(如materialdesigninxaml的演示应用程序所示),但是问题是验证消息与PasswordBox重叠。

enter image description here

我尝试为所有TextBox元素添加底部填充,但这没有帮助。我能想到的唯一解决方案是在底部元素上添加顶部填充,但我不喜欢该解决方案,因为它弄乱了我的布局。

用户控制:

$("#optionbox1").change(function() {
  var optionValue = $(this).val();
  var url = [location.protocol, '//', location.host, location.pathname].join('');
window.location = url+"?options added=" + optionValue;
});

ValidationRule:

        <materialDesign:Card Padding="32" Margin="16" MaxWidth="500">
            <StackPanel>
                <TextBox x:Name="UsernameTextBox" materialDesign:HintAssist.Hint="Username"  MaxLength="20">
                    <TextBox.Text>
                        <Binding Path="Username" UpdateSourceTrigger="PropertyChanged">
                            <Binding.ValidationRules>
                                <rules:NotEmptyValidationRule ValidatesOnTargetUpdated="True" />
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>
                <PasswordBox x:Name="PasswordBox" materialDesign:HintAssist.Hint="Password" MaxLength="10" />
            </StackPanel>
        </materialDesign:Card>

本质上,我想要的是为这些验证消息保留空间,而不是通过最小的代码修改将它们显示在TextBox下面的元素上(我不想为每个元素手动添加填充/边距)。 / p>

2 个答案:

答案 0 :(得分:1)

您可以在Style中创建全局App.xaml,在其中为所有TextBoxes提供一定的底边距:

<Style TargetType="TextBox" BasedOn="{StaticResource MaterialDesignTextBox}">
    <Setter Property="Margin" Value="0,0,0,15" />
</Style>

答案 1 :(得分:0)

当弹出选项choosen验证消息放置可以被改变。

private void coordDGV_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            double result;
            if (e.ColumnIndex >= 3)
            {
                if (!Double.TryParse(coordDGV.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue.ToString(), out result))
                {
                    e.Cancel = true;
                    coordDGV.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "Needs to be positive or negative decimal";
                }
                else
                {
                    coordDGV.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "";
                }
            }

        }    
相关问题