2

I wrote a sample to see if binding could be used within a Style in a blank Windows Store app - it compiled but did not work exactly as I'd hoped. I'm relatively new to XAML and binding so may have missed something.

In the sample below there are two rectangles, both bound to the slider control and both should change at the same time as the slider is moved, but it seems that only the first one changes; the first one is bound directly, the second is bound via a style.

Is binding in a Style supposed to be possible in a Win Store app? (My aim is to have a slider that changes the settings on a large number of elements at once, it seemed like this would be a better approach than copy/pasting bindings to all of them)

<Grid Background="#FF87873D">

    <StackPanel>
        <StackPanel.Resources>
            <Style x:Key="myTestRectangleStyle" TargetType="Rectangle">
                <Setter Property="Fill" Value="DarkBlue" />
                <Setter Property="Margin" Value="10,10" />
                <Setter Property="Height" Value="30" />
                <Setter Property="Width" Value="{Binding ElementName=slider1, Path=Value}" />
            </Style>
        </StackPanel.Resources>

        <Rectangle Width="{Binding ElementName=slider1, Path=Value}" Fill="Black" Margin="10,10" Height="30"/>

        <Rectangle Style="{StaticResource myTestRectangleStyle}"/>

        <Slider Name="slider1" Minimum="20" Maximum="200" Margin="20,0"/>
    </StackPanel>
</Grid>
terrybozzio
  • 4,424
  • 1
  • 19
  • 25
Tofa
  • 21
  • 4
  • Try setting the `DataContext` so that binding knows what type it is looking up the Visual Tree for. – GEEF Jul 11 '14 at 22:45
  • Thanks for your response! I had a bit of a read around `DataContext` and I've tried a few options as to where to set that but with no change, e.g. after `` I tried `` ``. Am I adding the DataContext into the correct place? – Tofa Jul 12 '14 at 09:58

1 Answers1

0

Answering my own question...it seems this isn't possible on a Windows Store App.

I had a clarification from a user on a MSDN forum that

[Bindings] are not supported on Style setters in Windows Store Apps like they are in WPF, i.e. you cannot bind to the Value property of the Slider in the Style

So the workaround is just to set the binding directly outside of the Style (a long winded option if you have a lot of elements to bind unfortunately)

Tofa
  • 21
  • 4