0

i just experiment with some binding, resources and styles in Visual Studio 2019. I observed some weird behaviour and I appreciate if somebody will be able get me explanation. I have these resources

    <DrawingImage x:Key="SomeIcon">
        <DrawingImage.Drawing>
            <DrawingGroup>
                <GeometryDrawing Brush="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1,AncestorType=Window}}" Geometry="F1 M 18.319,12.000 C 18.781,12.000 19.174,12.146 19.498,12.438 C 19.822,12.730 19.984,13.084 19.984,13.501 L 19.984,22.501 C 19.984,22.918 19.822,23.272 19.498,23.563 C 19.174,23.855 18.781,24.001 18.319,24.001 L 1.665,24.001 C 1.203,24.001 0.810,23.855 0.486,23.563 C 0.162,23.272 0.000,22.918 0.000,22.501 L 0.000,13.501 C 0.000,13.084 0.162,12.730 0.486,12.438 C 0.810,12.146 1.203,12.000 1.665,12.000 L 2.220,12.000 L 2.220,7.000 C 2.220,5.073 2.981,3.425 4.502,2.055 C 6.022,0.685 7.853,0.000 9.992,0.000 C 12.131,0.000 13.962,0.685 15.482,2.055 C 17.003,3.425 17.764,5.073 17.764,7.000 C 17.764,7.271 17.654,7.506 17.434,7.703 C 17.214,7.901 16.954,8.000 16.653,8.000 L 15.543,8.000 C 15.242,8.000 14.982,7.901 14.762,7.703 C 14.543,7.506 14.433,7.271 14.433,7.000 C 14.433,5.896 13.999,4.953 13.132,4.172 C 12.264,3.391 11.218,3.000 9.992,3.000 C 8.766,3.000 7.720,3.391 6.852,4.172 C 5.985,4.953 5.551,5.896 5.551,7.000 L 5.551,12.000 L 18.319,12.000 Z"/>
            </DrawingGroup>
        </DrawingImage.Drawing>
    </DrawingImage>

    <Style x:Key="TestStyle2" TargetType="{x:Type CheckBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CheckBox}">
                    <Grid>
                        <Image Source="{StaticResource SomeIcon}" Stretch="Uniform"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="TestStyle3" TargetType="{x:Type CheckBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CheckBox}">
                    <Grid>
                        <Image Stretch="Uniform">
                            <Image.Source>
                                <DrawingImage >
                                    <DrawingImage.Drawing>
                                        <DrawingGroup>
                                            <GeometryDrawing Brush="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" Geometry="F1 M 18.319,12.000 C 18.781,12.000 19.174,12.146 19.498,12.438 C 19.822,12.730 19.984,13.084 19.984,13.501 L 19.984,22.501 C 19.984,22.918 19.822,23.272 19.498,23.563 C 19.174,23.855 18.781,24.001 18.319,24.001 L 1.665,24.001 C 1.203,24.001 0.810,23.855 0.486,23.563 C 0.162,23.272 0.000,22.918 0.000,22.501 L 0.000,13.501 C 0.000,13.084 0.162,12.730 0.486,12.438 C 0.810,12.146 1.203,12.000 1.665,12.000 L 2.220,12.000 L 2.220,7.000 C 2.220,5.073 2.981,3.425 4.502,2.055 C 6.022,0.685 7.853,0.000 9.992,0.000 C 12.131,0.000 13.962,0.685 15.482,2.055 C 17.003,3.425 17.764,5.073 17.764,7.000 C 17.764,7.271 17.654,7.506 17.434,7.703 C 17.214,7.901 16.954,8.000 16.653,8.000 L 15.543,8.000 C 15.242,8.000 14.982,7.901 14.762,7.703 C 14.543,7.506 14.433,7.271 14.433,7.000 C 14.433,5.896 13.999,4.953 13.132,4.172 C 12.264,3.391 11.218,3.000 9.992,3.000 C 8.766,3.000 7.720,3.391 6.852,4.172 C 5.985,4.953 5.551,5.896 5.551,7.000 L 5.551,12.000 L 18.319,12.000 Z"/>
                                        </DrawingGroup>
                                    </DrawingImage.Drawing>
                                </DrawingImage>
                            </Image.Source>
                        </Image>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

and code in window is simple

...
<CheckBox Style="{StaticResource TestStyle3}"/>
<Button Content="Change" Height="30" Click="Button_Click"/>
...

or

...
<CheckBox Style="{StaticResource TestStyle2}"/>
<Button Content="Change" Height="30" Click="Button_Click"/>
...

So Icon should has same color as window foreground. In event Button_click I just change window foreground color to be sure that binding works correctly. When I use first case with TestStyle3 it works, but in "XAML binding failures window" is binding error "Severity Code Count Data Context Type Binding Path Target Target Type Description Error 4 1 null Foreground GeometryDrawing.Brush Brush Cannot find source: RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1' "

In case 2 with TestStyle2 it doesn't works and in "XAML binding failures window" is same error.

I would like to know if it is OK to use binding in resources. It's look like it works in some cases, but in this particular case not.

Thank you Daril

Daril
  • 1
  • In general, no...binding in resources doesn't work. Exceptions include binding markup in templates, where the actual binding is resolved at runtime where the template is applied. But otherwise, the binding engine is looking for a framework element to provide context for the binding, which doesn't exist in the resource dictionary environment. See duplicate for explanation and possible work-arounds. – Peter Duniho Jun 04 '21 at 07:08
  • Thank you for respond. But Istill don't understand why this simple exmaple works . ``` ``` and in window ``` ``` It is binding in resources and it is not template. Thanks – Daril Jun 04 '21 at 11:46

0 Answers0