I have a WPF user control with its code behind file containing the initialization stuff.
I am using the user control as an activeX component inside a different application.
XAML for the control is constructed within the other application and is applied to the user control programatically.
Hyperlinks within the XAML are not working and I found that I need to add the RequestNavigate event handler to get the hyperlink working.
I have added the required event handler inside the code behind and added an x:Class attribute to XAML with the namespace and class name.
I get an error message that the RequestNavigate event handler is not found.
'Windows.Forms.Integration.WpfControl' does not contain a definition for 'Hyperlink_RequestNavigate' and no extension method 'Hyperlink_RequestNavigate' accepting a first argument of type 'Windows.Forms.Integration.WpfControl' could be found (are you missing a using directive or an assembly reference?)
Can anyone please help?
Here is my code behind:
- namespace Windows.Forms.Integration
- {
- public partial class WpfControl : UserControl
- {
- internal const string PREFIX = "WPFControl-";
- private string mstrXamlFile, mstrXaml;
- private bool mblnInitialized;
- private string mstrUid;
-
- private static IDictionary<string, WpfControl> sobjWPFControls = new Dictionary<string, WpfControl>(StringComparer.OrdinalIgnoreCase);
- private static IDictionary<string, ObjectInstance> sobjObjectInstances = new Dictionary<string, ObjectInstance>(StringComparer.OrdinalIgnoreCase);
-
- public string ErrorMessage { get; set; }
-
- public static string WpfControlAssemblyVersion { get { return AssemblyInfo.VersionNumber; } }
- public WpfControl()
- {
- InitializeComponent();
- mstrUid = PREFIX + Guid.NewGuid().ToString();
- sobjWPFControls.Add(mstrUid, this);
- InitializeXaml();
-
- mblnInitialized = true;
- lblErrorMessage.Text = ErrorMessage;
- }
-
- public void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
- {
- Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
- e.Handled = true;
- }
-
- private void InitializeXaml()
- {
- System.Windows.UIElement objUIElement = null;
- lblErrorMessage.Visible = false;
- try
- {
- if (!string.IsNullOrEmpty(mstrXamlFile))
- mstrXaml = File.ReadAllText(mstrXamlFile);
- if (!string.IsNullOrEmpty(mstrXaml))
- objUIElement = XamlReader.Parse(mstrXaml) as System.Windows.UIElement;
- }
- catch (XamlParseException exc)
- {
-
- }
-
-
- }
-
-
-
- }
-
-
- }
Here is a sample XAML:
- <UserControl
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:system="clr-namespace:System;assembly=mscorlib"
- xmlns:interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
- xmlns:ti="clr-namespace:Windows.Forms.Integration;assembly=Windows.Forms.Integration"
- xmlns:inlineImage="clr-namespace:Windows.Forms.Integration;assembly=Windows.Forms.Integration"
- x:Class="Windows.Forms.Integration.WpfControl"
- mc:Ignorable="d"
- Background="White">
- <UserControl.Resources>
- <RadialGradientBrush x:Key="LightGrayBackground" GradientOrigin="0.2,0.2" RadiusX="1" RadiusY="1">
- <GradientStop Color="White" Offset="0"/>
- <GradientStop Color="Gray" Offset="1"/>
- </RadialGradientBrush>
- <SolidColorBrush x:Key="MarketColor" Color="#FF6BBE6B"/>
- <SolidColorBrush x:Key="BidColor" Color="LightBlue"/>
- <SolidColorBrush x:Key="DesignColor" Color="#FF7C7CE2"/>
- <SolidColorBrush x:Key="BuildColor" Color="#FFCB94CB"/>
- <SolidColorBrush x:Key="InstallColor" Color="LightCoral"/>
- <SolidColorBrush x:Key="ServiceColor" Color="LightSalmon"/>
- <SolidColorBrush x:Key="ActivityBackground" Color="White"/>
-
- <ControlTemplate x:Key="TitleLabel" TargetType="Label">
- <Grid Margin="4">
- <Rectangle Stroke="Blue" StrokeThickness="0" RadiusX="4" RadiusY="4" Fill="{TemplateBinding Background}">
- <Rectangle.Effect>
- <DropShadowEffect />
- </Rectangle.Effect>
- </Rectangle>
- <Label Content="{TemplateBinding Content}" Foreground="White" Margin="4" HorizontalAlignment="Center" FontSize="16" />
- </Grid>
- </ControlTemplate>
-
- <ControlTemplate x:Key="ProcessExpander" TargetType="Expander">
- <Grid Margin="4">
- <Rectangle RadiusX="4" RadiusY="4" Fill="{TemplateBinding Background}">
- <Rectangle.Effect>
- <DropShadowEffect />
- </Rectangle.Effect>
- </Rectangle>
- <Expander Content="{TemplateBinding Content}">
- <Expander.Header>
- <Label Content="{TemplateBinding Header}" HorizontalAlignment="Center" FontSize="14" />
- </Expander.Header>
- </Expander>
- </Grid>
- </ControlTemplate>
-
- <ControlTemplate x:Key="ActivityLabel" TargetType="Label">
- <Border Background="White" Margin="4,1,4,1" Opacity="0.7">
- <TextBlock Text="{TemplateBinding Content}" TextWrapping="WrapWithOverflow" Margin="1" TextAlignment="Center" />
- </Border>
- </ControlTemplate>
-
- <ControlTemplate x:Key="ActivityLinkLabel" TargetType="Label">
- <Border Margin="2,2,2,2">
- <TextBlock VerticalAlignment="Center" Text="{TemplateBinding Content}" TextWrapping="Wrap" Margin="1" TextAlignment="Center" FontSize="2.5">
- <interactivity:Interaction.Behaviors>
- <ti:NavigateBehavior Name="{Binding Path=Name, RelativeSource={RelativeSource TemplatedParent}}" Url="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" />
- </interactivity:Interaction.Behaviors>
- </TextBlock>
- </Border>
- </ControlTemplate>
-
- </UserControl.Resources>
- <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
- <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
- <TextBlock Margin="4,10,4,4" Text="3.3.1.3 Generate sales forecast - A (10136) 3" TextWrapping="Wrap" FontWeight="Bold" FontSize="16" Foreground="#5F5FAF" />
- <FlowDocumentScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" HorizontalAlignment="Left" HorizontalContentAlignment="Left" Width="Auto">
- <FlowDocument TextAlignment="Left" FontFamily= 'Verdana' FontSize= '11' LineHeight= '5' PageWidth= '1000'>
- <Section xml:space="preserve" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
- <Paragraph>
- <Hyperlink NavigateUri="https://www.google.com" RequestNavigate="Hyperlink_RequestNavigate">test url</Hyperlink>
- </Paragraph>
- </Section>
- </FlowDocument>
- </FlowDocumentScrollViewer>
- <Viewbox VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
- <Grid HorizontalAlignment="Stretch">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto" />
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="*" />
- </Grid.ColumnDefinitions>
- <StackPanel Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
- <Canvas Grid.Row="1" Grid.Column="0" Margin="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="0.00" Height="0.00">
- <Canvas.LayoutTransform>
- <ScaleTransform ScaleX="3.50" ScaleY="3.50" />
- </Canvas.LayoutTransform>
- </Canvas>
- <TextBlock Margin="4" Width="Auto" Text="Version 1.0 | Status Open | Rendered 5/31/2016 07:09:24 am. Click Refresh to update the view." TextWrapping="Wrap" FontSize="12" TextAlignment="Left" />
- </StackPanel>
- </Grid>
- </Viewbox>
- </StackPanel>
- </ScrollViewer>
- </UserControl>