简体   繁体   English

如何访问 WPF 代码隐藏中 ContentControl DataTemplate 中的 object,或者如何在 xaml 中设置 CefSharp DownloadHandler

[英]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:我在一个项目中有一个 XAML 文件,我将其抽象为:

    <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.此代码的想法是如果ViewModel.IsBar为真,则在文本框中显示ViewModel.Bar ,否则导航到ViewModel.HTML - 显示 HTML 或可编辑文本框背后存在商业原因。

I am trying to get the CefSharp browser to behave in a certain way when I want to download a file.当我想下载文件时,我试图让 CefSharp 浏览器以某种方式运行。 When we were using a WebBrowser, a link to a Word document caused IE to appear and offer an Open/Save box.当我们使用 WebBrowser 时,指向 Word 文档的链接会导致 IE 出现并提供一个打开/保存框。 CefSharp does not handle this without being told what to do. CefSharp 不会在没有被告知要做什么的情况下处理这个问题。 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)虽然这提供了更多的功能,但这确实意味着我们已经从能够处理一系列文件退回到能够处理 CefSharp 可以原生显示的内容(即 HTML 和图像)

I have tried a few code snippets I have found online to get hold of CefBrowser in the codebehind.我尝试了一些我在网上找到的代码片段来获取代码隐藏中的CefBrowser 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.我能找到的最接近的是Bob ,但我找不到让FindName工作的方法——我发现的几乎每个片段都在谈论 ListViewItems 以及我没有的属性。

I am trying to get the object in codebehind so that I can attach myDownloadHandler to the browser:我正在尝试在代码隐藏中获取 object,以便我可以将myDownloadHandler附加到浏览器:

    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:我尝试的另一件事是直接在 xaml 中分配处理程序:

                    <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这与字符串和 IDownloadHandler 之间的转换有关

So, the questions are: 1: Is there any way I can pick up CefBrowser in the code behind?所以,问题是: 1:有什么办法可以在后面的代码中CefBrowser吗? 2: Is there any way I can attach the handler in the xaml? 2:有什么办法可以将处理程序附加到 xaml 中? 3: Is there a better approach I can take that doesn't break the functionality? 3:有没有更好的方法可以不破坏功能?

add Loaded event handler:添加加载的事件处理程序:

<wpf:ChromiumWebBrowser Loaded="BrowserLoaded" ...>

cast sender to ChromiumWebBrowser type and assign DownloadHandler:sender转换为 ChromiumWebBrowser 类型并分配 DownloadHandler:

private void BrowserLoaded(object sender, RoutedEventArgs e)
{
    var browser = sender as ChromiumWebBrowser;
    browser.DownloadHandler = myDownloadHandler;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在使用WPF,我有DataTemplate,我想访问如何使用此代码的代码? - I am using WPF and I have DataTemplate that is i want to access into the codebehind how I can use this? 如何在FlipView中访问XAML DataTemplate内部的图像控件 - How do I access a image control inside a XAML DataTemplate in a FlipView 如何访问XAML DataTemplate中的控件? - How do I access a control inside a XAML DataTemplate? 如何在XAML中使GridViewItem成为可拖动对象而不是DataTemplate? - How can I make a GridViewItem as a draggable object instead of a DataTemplate in XAML? WPF如何更改ContentControl内容 - WPF How can i change ContentControl content 如何在WPF中访问列表框中的数据模板中的控件 - How Can I Access to The Controls in DataTemplate in ListBox in WPF 如何在我的代码背后设置的Javascript中访问变量 - How do I access a variable in Javascript, set in my codebehind 我可以在代码背后的数组中访问XAML元素吗? - Can I access XAML elements in an array in the codebehind? 如何将XAML中的依赖项属性设置为代码隐藏中所需的值 - How do i override a dependency property set in XAML to the value i need to in codebehind 您如何访问数据绑定 ContentControl 的 DataTemplate 生成的内容? - How do you access the DataTemplate generated content of a databound ContentControl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM