Click here to Skip to main content
15,867,453 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: Custom Control Styling Pin
Kevin Marois5-Dec-22 14:10
professionalKevin Marois5-Dec-22 14:10 
QuestionWPF .Net Core Relay Command with Parameters Pin
Kevin Marois1-Dec-22 13:50
professionalKevin Marois1-Dec-22 13:50 
QuestionForgot Password Pin
Kevin Marois1-Dec-22 13:06
professionalKevin Marois1-Dec-22 13:06 
AnswerRe: Forgot Password Pin
Richard Deeming1-Dec-22 22:10
mveRichard Deeming1-Dec-22 22:10 
QuestionWPF Core Hyperlkink Custom Control Pin
Kevin Marois29-Nov-22 16:29
professionalKevin Marois29-Nov-22 16:29 
I'm trying to create what I thought would be a simple custom control to implement a hyperlink.

This all works in WPF .Net Framework, but in .Net Core I have 2 problems. First, here's the code:

XAML
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:MaroisHyperlink"
                    xmlns:i="http://schemas.microsoft.com/xaml/behaviors">

<pre>
<Style TargetType="{x:Type local:MaroisHyperlink}">

    <Setter Property="Template">

        <Setter.Value>

            <ControlTemplate>

                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">

                    <TextBlock>

                        <Hyperlink>

                             <TextBlock Text="{Binding LinkText}"/>

                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Click">
                                    <i:InvokeCommandAction Command="{Binding LinkClickedCommand}"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>

                        </Hyperlink>

                    </TextBlock>

                </Border>

            </ControlTemplate>

        </Setter.Value>

    </Setter>

</Style>




Code Behind

using Marois.Framework.Core.Utilities;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace MaroisHyperlink
{
    public class MaroisHyperlink : Control
    {
        #region Commands
        private ICommand? _LinkClickedCommand;
        public ICommand LinkClickedCommand
        {
            get
            {
                if (_LinkClickedCommand == null)
                    _LinkClickedCommand = new RelayCommand(LinkClickedExecuted, LinkClickedCanExecute);
                return _LinkClickedCommand;
            }
        }
        #endregion

        #region DP LinkText
        public static readonly DependencyProperty LinkTextProperty =
                    DependencyProperty.Register("LinkText",
                    typeof(string),
                    typeof(MaroisHyperlink),
                    new PropertyMetadata("", new PropertyChangedCallback(OnLinkTextChanged)));

        public string LinkText
        {
            get { return (string)GetValue(LinkTextProperty); }
            set { SetValue(LinkTextProperty, value); }
        }

        private static void OnLinkTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //var control = (MaroisHyperlink)d;
        }
        #endregion

        #region LinkClickedEvent
        public static readonly RoutedEvent LinkClickedEvent =
                    EventManager.RegisterRoutedEvent("LinkClicked",
                    RoutingStrategy.Bubble,
                    typeof(RoutedEventHandler),
                    typeof(MaroisHyperlink));

        public event RoutedEventHandler LinkClicked
        {
            add { AddHandler(LinkClickedEvent, value); }
            remove { RemoveHandler(LinkClickedEvent, value); }
        }

        private void RaiseLinkClickedEvent()
        {
            RaiseEvent(new RoutedEventArgs(LinkClickedEvent));
        }
        #endregion

        #region CTOR
        static MaroisHyperlink()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MaroisHyperlink),
                new FrameworkPropertyMetadata(typeof(MaroisHyperlink)));
        }
        #endregion

        #region Private Methods
        private bool LinkClickedCanExecute()
        {
            return IsEnabled;
        }

        private void LinkClickedExecuted()
        {
            RaiseLinkClickedEvent();
        }
        #endregion
    }
}

Window

<Window x:Class="HyperlinkDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:link="clr-namespace:MaroisHyperlink;assembly=MaroisHyperlink"
        WindowStartupLocation="CenterScreen"
        mc:Ignorable="d"
        Title="MainWindow" 
        Height="300" 
        Width="500">

<pre>
<Grid>

    <link:MaroisHyperlink LinkText="Click This Link!"
                          HorizontalAlignment="Center"
                          VerticalAlignment="Center"
                          FontSize="16"/>

</Grid>




Problems

1. The LinkText is not appearing. I get no binding errors, but when I run it, there's nothing showing. If I set the inner TextBox's text to something, then it shows up.
<TextBlock>

<pre>
<Hyperlink>

    <TextBlock Text="THIS SHOWS UP"/>

</Hyperlink>



2. This doesn't compile:
<i:Interaction.Triggers>
    <i:EventTrigger EventName="Click">
        <i:InvokeCommandAction Command="{Binding LinkClickedCommand}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>
I get
The type 'Interaction' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
I have package "Microsoft.Xaml.Behaviors.WPF (1.1.39") installed and my namespace is "xmlns:i="http://schemas.microsoft.com/xaml/behaviors"

Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.

AnswerRe: WPF Core Hyperlkink Custom Control Pin
Richard Deeming29-Nov-22 21:59
mveRichard Deeming29-Nov-22 21:59 
GeneralRe: WPF Core Hyperlkink Custom Control Pin
Kevin Marois30-Nov-22 5:46
professionalKevin Marois30-Nov-22 5:46 
GeneralRe: WPF Core Hyperlkink Custom Control Pin
Richard Deeming30-Nov-22 21:25
mveRichard Deeming30-Nov-22 21:25 
QuestionPath Images Pin
Kevin Marois29-Nov-22 14:57
professionalKevin Marois29-Nov-22 14:57 
AnswerRe: Path Images Pin
Richard Deeming29-Nov-22 23:46
mveRichard Deeming29-Nov-22 23:46 
QuestionDropShadowEffect Above & Below Pin
Kevin Marois29-Nov-22 14:30
professionalKevin Marois29-Nov-22 14:30 
AnswerRe: DropShadowEffect Above & Below Pin
Richard Deeming29-Nov-22 23:43
mveRichard Deeming29-Nov-22 23:43 
GeneralRe: DropShadowEffect Above & Below Pin
Kevin Marois30-Nov-22 5:32
professionalKevin Marois30-Nov-22 5:32 
GeneralRe: DropShadowEffect Above & Below Pin
Richard Deeming30-Nov-22 21:30
mveRichard Deeming30-Nov-22 21:30 
QuestionWhat's Wrong With This Style? Pin
Kevin Marois21-Nov-22 9:57
professionalKevin Marois21-Nov-22 9:57 
AnswerRe: What's Wrong With This Style? Pin
Gerry Schmitz21-Nov-22 10:17
mveGerry Schmitz21-Nov-22 10:17 
GeneralRe: What's Wrong With This Style? Pin
Kevin Marois21-Nov-22 11:44
professionalKevin Marois21-Nov-22 11:44 
QuestionStyle Question Pin
Kevin Marois20-Nov-22 10:30
professionalKevin Marois20-Nov-22 10:30 
AnswerRe: Style Question Pin
Richard Deeming20-Nov-22 22:37
mveRichard Deeming20-Nov-22 22:37 
GeneralRe: Style Question Pin
Kevin Marois21-Nov-22 8:05
professionalKevin Marois21-Nov-22 8:05 
Questionediting a WPF DataGrid Pin
pitwi13-Nov-22 9:08
pitwi13-Nov-22 9:08 
AnswerRe: editing a WPF DataGrid Pin
Gerry Schmitz15-Nov-22 7:38
mveGerry Schmitz15-Nov-22 7:38 
QuestionCan I see the bounding box around WPF controls at runtime ? Pin
Maximilien27-Oct-22 3:16
Maximilien27-Oct-22 3:16 
AnswerRe: Can I see the bounding box around WPF controls at runtime ? Pin
Richard Deeming27-Oct-22 4:05
mveRichard Deeming27-Oct-22 4:05 
QuestionCustom drawing ToggleButton when checked Pin
Maximilien17-Oct-22 9:12
Maximilien17-Oct-22 9:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.