'How can I access an object that is in a ContentControl DataTemplate in WPF codebehind, or how do I set a CefSharp DownloadHandler in xaml

I have a XAML file in a project, which I have abstracted down to this:

    <Grid>
        <ContentControl KeyboardNavigation.IsTabStop="False" Content="{Binding }" Grid.Row="8" Grid.ColumnSpan="5" x:Name="Bob">
            <ContentControl.Resources>
                <DataTemplate x:Key="FooView">
                    <wpf:ChromiumWebBrowser x:Name="CefBroswer"
                    Address="{Binding Path=HTML}">
                </DataTemplate>
                <DataTemplate x:Key="BarView">
                    <TextBox Text="{Binding Path=Bar}"></TextBox>
                </DataTemplate>
            </ContentControl.Resources>

            <ContentControl.Style>
                <Style TargetType="{x:Type ContentControl}">
                    <Setter Property="ContentTemplate" Value="{StaticResource FooView}"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsBar}" Value="True">
                            <Setter Property="ContentTemplate" Value="{StaticResource BarView}"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ContentControl.Style>
        </ContentControl>

    </Grid>

The idea of this code is to display ViewModel.Bar in a textbox if ViewModel.IsBar is true, otherwise to navigate to ViewModel.HTML - there are business reasons behind displaying either HTML or an editable text box.

I am trying to get the CefSharp browser to behave in a certain way when I want to download a file. When we were using a WebBrowser, a link to a Word document caused IE to appear and offer an Open/Save box. CefSharp does not handle this without being told what to do. While this gives a lotmore power, it does mean that we have regressed from being able to work with a range of files to being able to work with things that CefSharp can natively display (ie HTML and images)

I have tried a few code snippets I have found online to get hold of CefBrowser in the codebehind. The closest I can get to is Bob, and I cannot find a way to get FindName to work - pretty much every snippet I found talks about ListViewItems, and properties off of that which I do not have.

I am trying to get the object in codebehind so that I can attach myDownloadHandler to the browser:

    CefSettings settings = new CefSettings();
    Cef.Initialize(settings);

    CefBrowser.DownloadHandler = myDownloadHandler; //This is what documentation says to use for this
    DataTemplate bob = Bob.FindResource("FooView") as DataTemplate;

The other thing I have tried is to assign the handler in the xaml directly:

                    <wpf:ChromiumWebBrowser x:Name="CefBroswer"
                    Address="{Binding Path=HTML}"
                    DownloadHandler="{Binding Path=myDownloadHandler}">

This upsets it - I assume that it is binding to the result of the handler, rather than the handler itself, and that confuses things

                    <wpf:ChromiumWebBrowser x:Name="CefBroswer"
                    Address="{Binding Path=HTML}"
                    DownloadHandler="MyDownloadHandler">

This also didn't work, and was giving intellisense errors about converting from a string. Of course, now I have put that back it to try and copy the message, it's not showing it. It was to do with converting between string and IDownloadHandler

So, the questions are: 1: Is there any way I can pick up CefBrowser in the code behind? 2: Is there any way I can attach the handler in the xaml? 3: Is there a better approach I can take that doesn't break the functionality?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source