1

I'm trying to bind to the DataContext of an element from within a Style, which for some reason is resulting in a 'catastrophic failure' in the XAML parser. Here is the code:

<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="&#xE700;"/>
                    </Button.DataContext>
                </Button>
            </Grid>
        </SplitView.Pane>
    </SplitView>
</UserControl>

When I attempt to compile this in Visual Studio, this is the error message that results:

I've tried mucking around with the Bindings a bit and changing the RelativeSource, but to no avail; the same error message pops up each time.

Why does this happen, and what can I do to fix it?

Romasz
  • 29,662
  • 13
  • 79
  • 154
James Ko
  • 32,215
  • 30
  • 128
  • 239

1 Answers1

2

Binding in Setter's Value is not supported in Windows Runtime - take a look 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.

You may also look here at Clemens answer for a workaround.

Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154