我的风格很好用
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:Downloader.App.ResourceDictionaries.NamedStyles">
<Style x:Key="TextBoxWithDefaultTextStyle" TargetType="TextBox">
<!--Padding for typed text-->
<Setter Property="Padding" Value="5, 2, 5, 0"/>
<Setter Property="Background" Value="White" />
<Setter Property="BorderBrush" Value="#FF858585" />
<Setter Property="BorderThickness" Value="3" />
<Setter Property="Border.CornerRadius" Value="10" />
<Setter Property="Control.Template" Value="{StaticResource TextBoxBaseControlTemplate}"></Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel>
<Border BorderBrush="Brown" BorderThickness="5"
CornerRadius="2">
<AdornedElementPlaceholder x:Name="controlWithError"/>
</Border>
<TextBlock Foreground="Red" FontSize="20" FontFamily="Segoe UI" Margin="3,0,0,0">!</TextBlock>
</DockPanel>
<ControlTemplate.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Background" Value="Red"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
然后我想把它放在与普通模板
一样的独立资源中<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:Downloader.App.ResourceDictionaries.NamedStyles">
<Style x:Key="TextBoxWithDefaultTextStyle" TargetType="TextBox">
<!--Padding for typed text-->
<Setter Property="Padding" Value="5, 2, 5, 0"/>
<Setter Property="Background" Value="White" />
<Setter Property="BorderBrush" Value="#FF858585" />
<Setter Property="BorderThickness" Value="3" />
<Setter Property="Border.CornerRadius" Value="10" />
<Setter Property="Control.Template" Value="{StaticResource TextBoxBaseControlTemplate}"></Setter>
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource TextBoxValidationTemplate}"></Setter>
</Style>
</ResourceDictionary>
TextBoxValidationTemplate.xaml:
<AdornedElementPlaceholder x:Name="controlWithError"/>
</Border>
<TextBlock Foreground="Red" FontSize="20" FontFamily="Segoe UI" Margin="3,0,0,0">!</TextBlock>
</DockPanel>
<ControlTemplate.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Background" Value="Red"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
然后我运行应用程序并输入无效数据并获取异常: 的&#39; {DependencyProperty.UnsetValue}&#39;不是属性的有效值&#39; ErrorTemplate&#39;。
如果我直接应用错误模板
<TextBox Text="{Binding UserName, Mode=TwoWay, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}"
Validation.ErrorTemplate="{StaticResource TextBoxValidationTemplate}"
Style="{StaticResource TextBoxWithDefaultTextStyle}"
Grid.Row="2"
Grid.Column="0"
Margin="5,2,5,2"
Grid.ColumnSpan="2"/>
它也可以,所以文件没问题
答案 0 :(得分:0)
找到我自己。模板必须先行,然后是样式
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:Downloader.App.ViewModel" />
</ResourceDictionary>
<ResourceDictionary Source="ResourceDictionaries\Templates\TextBoxBaseControlTemplate.xaml" />
<ResourceDictionary Source="ResourceDictionaries\Templates\TextBoxValidationTemplate.xaml" />
<ResourceDictionary Source="ResourceDictionaries\Templates\PasswordBoxControlTemplate.xaml" />
<ResourceDictionary Source="ResourceDictionaries\NamedStyles\TextBoxWithDefaultTextStyle.xaml" />
<ResourceDictionary Source="ResourceDictionaries\NamedStyles\PasswordBoxStyle.xaml" />
<ResourceDictionary Source="ResourceDictionaries\Images\Common.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>