我有一种基础风格,以及基于该风格的一系列风格。它们都使用了一些(相同的)触发器,它们运行良好 - 除了一个触发器。这个特定的只适用于派生样式。
尝试将此触发器直接移动到基本样式会导致Visual Studio Designer中出现NullReferenceException。任何人都可以告诉我为什么(以及如何使其工作)?
<Style x:Key="CalendarCellStyle" TargetType="{x:Type TextBlock}">
<Setter Property="Text" Value="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"/>
<Setter Property="FontSize" Value="{StaticResource FontSize9}"/>
<Setter Property="TextAlignment" Value="Center"/>
<Setter Property="Margin" Value="5"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="Foreground" Value="#FF515151"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<EventSetter Event="MouseDown" Handler="MouseDown_Click" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="Red" />
</Trigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled}" Value="False">
<Setter Property="Foreground" Value="{StaticResource ColorCalendarForegroundDisabled}" />
<Setter Property="Background" Value="{StaticResource ColorCalendarBackgroundDisabled}" />
</DataTrigger>
<!-- the following trigger HERE causes a NullReferenceException -->
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource EqualsConverter}">
<Binding RelativeSource="{RelativeSource Self}" Path="Tag" />
<Binding RelativeSource="{RelativeSource AncestorType={x:Type UniformGrid}}" Path="Tag" />
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Foreground" Value="{StaticResource ColorCalendarForegroundSelected}" />
<Setter Property="Background" Value="{StaticResource ColorCalendarBackgroundSelected}" />
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="CalendarCellStyleMonth" BasedOn="{StaticResource CalendarCellStyle}" TargetType="{x:Type TextBlock}">
<Setter Property="Text" Value="{Binding RelativeSource={RelativeSource Self}, Path=Tag, Converter={StaticResource MonthNameConverter}}"/>
<Setter Property="FontSize" Value="{StaticResource FontSize12}"/>
<Style.Triggers>
<!-- the very same trigger code HERE works properly ... -->
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource EqualsConverter}">
<Binding RelativeSource="{RelativeSource Self}" Path="Tag" />
<Binding RelativeSource="{RelativeSource AncestorType={x:Type UniformGrid}}" Path="Tag" />
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Foreground" Value="{StaticResource ColorCalendarForegroundSelected}" />
<Setter Property="Background" Value="{StaticResource ColorCalendarBackgroundSelected}" />
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="CalendarCellStyleDay" BasedOn="{StaticResource CalendarCellStyle}" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<!-- ... as well as here too (and so on) -->
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource EqualsConverter}">
<Binding RelativeSource="{RelativeSource Self}" Path="Tag" />
<Binding RelativeSource="{RelativeSource AncestorType={x:Type UniformGrid}}" Path="Tag" />
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Foreground" Value="{StaticResource ColorCalendarForegroundSelected}" />
<Setter Property="Background" Value="{StaticResource ColorCalendarBackgroundSelected}" />
</DataTrigger>
</Style.Triggers>
</Style>
不幸的是,堆栈跟踪并没有给出任何线索:
bei DataTest.EqualsConverter.Convert(Object [] values,Type targetType,Object parameter,CultureInfo culture) bei System.Windows.Data.MultiBindingExpression.TransferValue() bei System.Windows.Data.MultiBindingExpression.Transfer() bei System.Windows.Data.MultiBindingExpression.UpdateTarget(Boolean includeInnerBindings) bei System.Windows.Data.MultiBindingExpression.AttachToContext(Boolean lastChance) bei System.Windows.Data.MultiBindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance) bei MS.Internal.Data.DataBindEngine.Task.Run(布尔值lastChance) bei MS.Internal.Data.DataBindEngine.Run(Object arg) bei MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender,EventArgs e) bei System.Windows.ContextLayoutManager.fireLayoutUpdateEvent() bei System.Windows.ContextLayoutManager.UpdateLayout() bei System.Windows.UIElement.UpdateLayout()
答案 0 :(得分:0)
问题解决了;这只是因为切割和粘贴过程本身。
将代码snipet复制到外部文本编辑器后,再次从那里返回基本样式后,异常就消失了。
我认为这是Visual Studio本身的一个错误,因为最初(我忘了提及)甚至重建整个项目并没有解决问题。
对不起。