开发者

WPF TabItem Desire Binding to Only Occur When IsSelected True

开发者 https://www.devze.com 2023-04-06 17:14 出处:网络
The contents of one TabItem is large CPU expensive to create.Would like to only get the content via binding for the TabItem when IsSelected = True.Or get alternate content based on IsSelected True or

The contents of one TabItem is large CPU expensive to create. Would like to only get the content via binding for the TabItem when IsSelected = True. Or get alternate content based on IsSelected True or False. Giving up IsAsync = True in this case is not an option.

The XAML below works but it retrieves the content regardless if IsSeleted = False or IsSelected = True.

    <TabItem IsSelected="False"  Header="Expensive Content">
        <FlowDocumentReader Name="FlowDocumentPageViewer1" HorizontalAlignment="Stretch" DataContext="{Binding Source={x:Static Application.Current}}">
            <FlowDocumentReader.Document>
                    <Binding Path="MyGabeLib.Search.SelectedDoc.XAMLdocFlowDocument" IsAsync="True" Converter="{StaticResource 开发者_运维技巧flowDocumentToXamlConverter}" 
                        FallbackValue="{StaticResource DefaultFlowDoc}" Mode="OneWay"/>
            </FlowDocumentReader.Document>                                         
         </FlowDocumentReader>
    </TabItem>

The converter is to convert a string (serialized using XamlWriter.Save(DocFlowDocument)) back to a FlowDocument as cannot use IsAsync = True (directly) with a FlowDocument.

Binding to a property in the code behind to redirect did not work as the UI with IsAcync = True is on another thread and does not have access to IsSelected.

Was hoping to use a template with a trigger but this is as far as I got. Not very far but it does not throw a syntax error.

    <TabItem IsSelected="False" Name="TabItemFlowDoc">        
        <FlowDocumentReader ...>
            <FlowDocumentReader.Template>
                <ControlTemplate>            
                </ControlTemplate>    
            </FlowDocumentReader.Template>
         </FlowDocumentReader>

Tried trigger directly on the TabControl and could not even get past syntax errors

    <TabControl Grid.Row="0" Grid.Column="0" Name="TabControlView">
        <TabControl.Resources>
            <DataTemplate>
                <FlowDocumentReader.Template>                   
                </FlowDocumentReader.Template>
            </DataTemplate>
        </TabControl.Resources>

Thanks in advance.


So what I ended up doing was fetching the document text and other parameter for marking it up on a background thread. Then on the UI thread I check the TabIndex if that tab is selected and only produce the FlowDocument if that tab is selected. Another tab presents the first 4 thousand characters of the text so the user can quickly decide if they want to render the full highlighted documents.

0

精彩评论

暂无评论...
验证码 换一张
取 消