How can I replace the style of a DocumentViewer ?

Platform: WPF| Category: DocumentViewer

This can be done with the following code snippets,

Define the required styles in the App.xaml file.

[XAML]

<Style 
      x:Key="MyDVStyleExtend1"
      BasedOn="{StaticResource {x:Type DocumentViewer}}"
      TargetType="{x:Type DocumentViewer}">
    <Setter Property="Background">
        <Setter.Value>
            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                <GradientStop Offset="0.0" Color="Red" />
                <GradientStop Offset="1.0" Color="White" />
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
</Style>
<Style 
      x:Key="MyDVStyleExtend2"
      BasedOn="{StaticResource {x:Type DocumentViewer}}"
      TargetType="{x:Type DocumentViewer}">
    <Setter Property="Background">
        <Setter.Value>
            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                <GradientStop Offset="0.0" Color="Blue" />
                <GradientStop Offset="1.0" Color="White" />
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
</Style>

Define the DocumentViewer in MainWindow.

[XAML]

<Grid>
    <DocumentViewer  Name="MyDocumentViewer"/>
    <Button Content="Style1" Height="30" Width="100" Click="Button1_Click" />
    <Button Content="Style2" Height="30" Width="100" Click="Button2_Click" />           
</Grid>

Replace the styles for document viewer based on button clicks from main window.

[C#]
private void Button1_Click(object sender, RoutedEventArgs e)
{
    MyDocumentViewer.Style = (Style)Application.Current.Resources['MyDVStyleExtend1'];
}
 
private void Button2_Click(object sender, RoutedEventArgs e)
{
    MyDocumentViewer.Style = (Style)Application.Current.Resources['MyDVStyleExtend2'];
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.