簡體   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

我在一個項目中有一個 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>

此代碼的想法是如果ViewModel.IsBar為真,則在文本框中顯示ViewModel.Bar ,否則導航到ViewModel.HTML - 顯示 HTML 或可編輯文本框背后存在商業原因。

當我想下載文件時,我試圖讓 CefSharp 瀏覽器以某種方式運行。 當我們使用 WebBrowser 時,指向 Word 文檔的鏈接會導致 IE 出現並提供一個打開/保存框。 CefSharp 不會在沒有被告知要做什么的情況下處理這個問題。 雖然這提供了更多的功能,但這確實意味着我們已經從能夠處理一系列文件退回到能夠處理 CefSharp 可以原生顯示的內容(即 HTML 和圖像)

我嘗試了一些我在網上找到的代碼片段來獲取代碼隱藏中的CefBrowser 我能找到的最接近的是Bob ,但我找不到讓FindName工作的方法——我發現的幾乎每個片段都在談論 ListViewItems 以及我沒有的屬性。

我正在嘗試在代碼隱藏中獲取 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;

我嘗試的另一件事是直接在 xaml 中分配處理程序:

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

這讓它感到不安 - 我假設它綁定到處理程序的結果,而不是處理程序本身,這讓事情變得混亂

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

這也不起作用,並且給出了關於從字符串轉換的智能感知錯誤。 當然,現在我把它放回去嘗試復制消息,它沒有顯示它。 這與字符串和 IDownloadHandler 之間的轉換有關

所以,問題是: 1:有什么辦法可以在后面的代碼中CefBrowser嗎? 2:有什么辦法可以將處理程序附加到 xaml 中? 3:有沒有更好的方法可以不破壞功能?

添加加載的事件處理程序:

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

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM