[英]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.