页面内控件未显示错误模板

3

我的页面中有一个文本框,绑定到一个经过验证的模型属性上,但当出现错误时,错误模板不会显示!

以下是几点说明:

  1. 对于在窗口内而非在窗口的框架中显示的页面内的控件,我没有验证问题。
  2. 错误模板有效,在窗口内的控件中显示。
  3. 模型正在验证,因为在识别到验证错误时,“保存”按钮将被禁用。

“我认为”问题在于该控件位于嵌套框架中的页面内,因此数据上下文似乎未被传递到该页面。这可能是原因吗?如果是,我该如何解决此问题?如果不是,还可能是什么问题?

代码(我当然已经简化了代码以隔离问题):

<Page x:Class="PIRS_Client.View.Staff.StaffDetailsView"
    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:ignore="http://www.ignore.com"
    mc:Ignorable="d ignore"
    DataContext="{Binding StaffDetailsVM, Source={StaticResource Locator}}"
    Height="576" Width="1163">

<Grid>        
    <TextBox HorizontalAlignment="Left" Text="{Binding Model.title, ValidatesOnDataErrors=True}" Height="17" Margin="284,453,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="90"/>
    <Button Content="Save Changes" Command="{Binding SaveDetailsCommand}" IsEnabled="{Binding Model.IsValid}" HorizontalAlignment="Left" Margin="1007,518,0,0" VerticalAlignment="Top" Width="104" Height="23"/>
</Grid>

如果我能添加任何其他信息或代码,请告诉我!


如果您在文本框中输入无效数据并单击它以外的区域,则会出现验证错误吗? - gleng
@gleng 是的,错误已记录(在我调试时),按钮被禁用(显示有验证错误),当我输入有效文本时,按钮会重新启用。 - Sam
1
@gleng - 没问题,感谢您抽出时间尝试帮助我解决这个问题! - Sam
1个回答

0
问题解决了 - 原来Windows有一个内置的装饰层,但页面没有,我很想给你一个详细的解释为什么没有,但我不知道为什么(请参阅下文)。但解决方案是,您需要在装饰器装饰器中包装页面内容,以为它们提供装饰层。
只是为了清楚起见,这里是“修复”的代码:
<Page x:Class="PIRS_Client.View.Staff.StaffDetailsView"
    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:ignore="http://www.ignore.com"
    mc:Ignorable="d ignore"
    DataContext="{Binding StaffDetailsVM, Source={StaticResource Locator}}"
    Height="576" Width="1163">

<AdornerDecorator>
    <Grid>        
        <TextBox HorizontalAlignment="Left" Text="{Binding Model.title, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Height="17" Margin="284,453,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="90"/>
        <Button Content="Save Changes" Command="{Binding SaveDetailsCommand}" IsEnabled="{Binding Model.IsValid}" HorizontalAlignment="Left" Margin="1007,518,0,0" VerticalAlignment="Top" Width="104" Height="23"/>
    </Grid>
</AdornerDecorator>

如果您知道为什么页面不实现装饰器层并能够提供一个更深入的答案,请随时添加解释作为另一个答案,其中包含解决方案,例如这个和解释,我将把您的答案更改为正确答案(而不是我的)- 仅仅是为了促进分享知识;)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接