绑定到 Style 中的 DataContext 会导致 'catastrophic failure?'
Binding to the DataContext within a Style results in a 'catastrophic failure?'
我正在尝试从 Style
中绑定到元素的 DataContext
,由于某种原因导致 'catastrophic failure' 在 XAML 中解析器。这是代码:
<UserControl
x:Class="Sirloin.AppView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Sirloin"> <!--A few lines omitted for brevity-->
<UserControl.Resources>
<ResourceDictionary>
<Style x:Key="MenuButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Transparent"/>
<!--This fails-->
<Setter Property="Content" Value="{Binding Symbol}"/>
<Setter Property="FontFamily" Value="Segoe MDL2 Assets"/>
<!--And this too-->
<Setter Property="Width" Value="{Binding CompactPaneLength, ElementName=splitView}"/>
</Style>
</ResourceDictionary>
</UserControl.Resources>
<SplitView x:Name="splitView" DisplayMode="CompactOverlay">
<SplitView.Pane>
<Grid>
<!--The hamburger-->
<Button Grid.Row="0" Style="{StaticResource MenuButtonStyle}">
<Button.DataContext>
<local:MenuItem Symbol=""/>
</Button.DataContext>
</Button>
</Grid>
</SplitView.Pane>
</SplitView>
</UserControl>
当我尝试在 Visual Studio 中编译时,这是导致的错误消息:
我试过稍微修改 Bindings
并更改 RelativeSource
,但无济于事;每次都弹出相同的错误消息。
为什么会发生这种情况,我该如何解决?
Setter 的值 中的绑定在Windows 运行时 中不受支持 - 看看at MSDN:
Windows Presentation Foundation (WPF) and Microsoft Silverlight supported the ability to use a Binding expression to supply the Value for a Setter in a Style. The Windows Runtime doesn't support a Binding usage for Setter.Value (the Binding won't evaluate and the Setter has no effect, you won't get errors, but you won't get the desired result either). When you convert XAML styles from WPF or Silverlight XAML, replace any Binding expression usages with strings or objects that set values, or refactor the values as shared {StaticResource} markup extension values rather than Binding-obtained values.
您也可以查看 解决方法。
我正在尝试从 Style
中绑定到元素的 DataContext
,由于某种原因导致 'catastrophic failure' 在 XAML 中解析器。这是代码:
<UserControl
x:Class="Sirloin.AppView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Sirloin"> <!--A few lines omitted for brevity-->
<UserControl.Resources>
<ResourceDictionary>
<Style x:Key="MenuButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Transparent"/>
<!--This fails-->
<Setter Property="Content" Value="{Binding Symbol}"/>
<Setter Property="FontFamily" Value="Segoe MDL2 Assets"/>
<!--And this too-->
<Setter Property="Width" Value="{Binding CompactPaneLength, ElementName=splitView}"/>
</Style>
</ResourceDictionary>
</UserControl.Resources>
<SplitView x:Name="splitView" DisplayMode="CompactOverlay">
<SplitView.Pane>
<Grid>
<!--The hamburger-->
<Button Grid.Row="0" Style="{StaticResource MenuButtonStyle}">
<Button.DataContext>
<local:MenuItem Symbol=""/>
</Button.DataContext>
</Button>
</Grid>
</SplitView.Pane>
</SplitView>
</UserControl>
当我尝试在 Visual Studio 中编译时,这是导致的错误消息:
我试过稍微修改 Bindings
并更改 RelativeSource
,但无济于事;每次都弹出相同的错误消息。
为什么会发生这种情况,我该如何解决?
Setter 的值 中的绑定在Windows 运行时 中不受支持 - 看看at MSDN:
Windows Presentation Foundation (WPF) and Microsoft Silverlight supported the ability to use a Binding expression to supply the Value for a Setter in a Style. The Windows Runtime doesn't support a Binding usage for Setter.Value (the Binding won't evaluate and the Setter has no effect, you won't get errors, but you won't get the desired result either). When you convert XAML styles from WPF or Silverlight XAML, replace any Binding expression usages with strings or objects that set values, or refactor the values as shared {StaticResource} markup extension values rather than Binding-obtained values.
您也可以查看