0

I just have a simple binding, it works well but there is an error popup.

The effects work but still an error.

And the error is System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:(no path); DataItem=null; target element is 'VisualBrush' (HashCode=23487194); target property is 'Visual' (type 'Visual')

I have tried x: Reference but there would be another error.

Appreciated a lot if any can help.

<Style TargetType="{x:Type Window}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Window}">
                        <Grid>
                            <Border 
                           x:Name="RoundMask"
                           CornerRadius="10"
                           Background="white"/>

                            <!-- The main content -->
                            <Grid>
                                <Grid.OpacityMask>
                                    <VisualBrush Visual="{Binding ElementName=RoundMask}" />
                                </Grid.OpacityMask>
                            </Grid>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

1 Answers1

0

Re templating a window like that is not really the way I'd go about this sort of thing.

I'd use something more like the approach in this sample:

https://gallery.technet.microsoft.com/ThWPFPolishing-Chrome-f41be7fe

Finished fancy window is enter image description here

Window6, using WindowChrome styling which is in the resource dictionary Dictionary1.

Which has stuff lika a big round close button. But to give you the idea before downloading:

<Style x:Key="FinishedWindow" TargetType="{x:Type Window}">
    <Setter Property="FontFamily" Value="Comic Sans MS"/>
    <Setter Property="Foreground" Value="{StaticResource DarkDark}"/>
    <Setter Property="WindowChrome.WindowChrome">
        <Setter.Value>
            <WindowChrome CaptionHeight="0"
                          CornerRadius="20"
                          GlassFrameThickness="0"
                          NonClientFrameEdges="None"
                          ResizeBorderThickness="5"
                                    />
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">
                <Grid>
                    <Border   Background="{StaticResource BrightMid}"   BorderBrush="{StaticResource DarkLight}" BorderThickness="4,4,6,6" 
                         CornerRadius="12">
                        <Border.Effect>
                            <BlurEffect  KernelType="Gaussian" Radius="12" RenderingBias="Quality" />
                        </Border.Effect>
                    </Border>
                    <Border BorderBrush="{StaticResource DarkDark}" BorderThickness="2" 
                            CornerRadius="12" ClipToBounds="True">
                    </Border>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="32"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Title}" 
                                          Foreground="{StaticResource DarkDark}"
                                          Grid.Row="0"
                                          HorizontalAlignment="Center" 
                                          VerticalAlignment="Bottom"
                                          FontWeight="Bold"
                                          FontSize="16"
                                     />
                        <Button Name="CloseButton" 
                                Width="20" Height="20"   
                                Grid.Row="0"
                                HorizontalAlignment="Right"
                                BorderThickness="0"
                                Margin="0,12,12,0"
                                Command="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CloseCommand}"
                                Style="{StaticResource CloseButton}"/>
                        <ContentPresenter Grid.Row="1" Margin="12"/>
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter> 

I gave your style a go.

Just using it implicitly had no effect at all.

I put it in app.xaml and gave it a key

<Application.Resources>
    <Style TargetType="{x:Type Window}" x:Key="roundedWindowStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                    <Grid>
                        <Border 
                       x:Name="RoundMask"
                       CornerRadius="10"
                       Background="white"/>

                        <!-- The main content -->
                        <Grid>
                            <Grid.OpacityMask>
                                <VisualBrush Visual="{Binding ElementName=RoundMask}" />
                            </Grid.OpacityMask>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

And then I applied that to mainwindow

<Window
    ...
    Title="MainWindow" 

    Style="{StaticResource roundedWindowStyle}"

Hit f5... and it kind of works. Well.

If you ignore that window chrome means it cannot work as you seem to intend.

enter image description here

You probably ought to be looking at using window chrome instead.

With what you have there.

At the absolute minimum, you need a Contentpresenter inside that Grid. Because a window is a content control but it won't show any content at all if you have no contentpresenter in the template.

Andy
  • 11,864
  • 2
  • 17
  • 20
  • Hi Thank you for the reply, but this actually isn't the issue that I am facing, I didn't include the content part and the chrome part, but the problem is caused by VisualBrush, as using visual brush binding in a style will cause an exception. – Will Zhang Mar 09 '19 at 19:44
  • See the second picture? Using the visual brush binding in a style. No exception. – Andy Mar 09 '19 at 20:05
  • Hi Andy thank you so much, I think I know why this happened. It's because I pack ed this style in a dll,. If I put it in the app.xaml it works well. But since it's a messagebox I still want to put it in a dll for referencing. – Will Zhang Mar 10 '19 at 01:23
  • And when the window inside the dll reference this style it will cause this error, but the other normal window can use this freely. – Will Zhang Mar 10 '19 at 01:44