Click here to Skip to main content
15,566,520 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: Custom Control Styling Pin
Kevin Marois5-Dec-22 13:15
professionalKevin Marois5-Dec-22 13:15 
GeneralRe: Custom Control Styling Pin
Richard Deeming5-Dec-22 22:57
mveRichard Deeming5-Dec-22 22:57 
GeneralRe: Custom Control Styling Pin
Kevin Marois5-Dec-22 14:57
professionalKevin Marois5-Dec-22 14:57 
GeneralRe: Custom Control Styling Pin
Kevin Marois5-Dec-22 15:10
professionalKevin Marois5-Dec-22 15:10 
QuestionWPF .Net Core Relay Command with Parameters Pin
Kevin Marois1-Dec-22 14:50
professionalKevin Marois1-Dec-22 14:50 
QuestionForgot Password Pin
Kevin Marois1-Dec-22 14:06
professionalKevin Marois1-Dec-22 14:06 
AnswerRe: Forgot Password Pin
Richard Deeming1-Dec-22 23:10
mveRichard Deeming1-Dec-22 23:10 
QuestionWPF Core Hyperlkink Custom Control Pin
Kevin Marois29-Nov-22 17:29
professionalKevin Marois29-Nov-22 17: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 22:59
mveRichard Deeming29-Nov-22 22:59 
GeneralRe: WPF Core Hyperlkink Custom Control Pin
Kevin Marois30-Nov-22 6:46
professionalKevin Marois30-Nov-22 6:46 
GeneralRe: WPF Core Hyperlkink Custom Control Pin
Richard Deeming30-Nov-22 22:25
mveRichard Deeming30-Nov-22 22:25 
QuestionPath Images Pin
Kevin Marois29-Nov-22 15:57
professionalKevin Marois29-Nov-22 15:57 
AnswerRe: Path Images Pin
Richard Deeming30-Nov-22 0:46
mveRichard Deeming30-Nov-22 0:46 
QuestionDropShadowEffect Above & Below Pin
Kevin Marois29-Nov-22 15:30
professionalKevin Marois29-Nov-22 15:30 
AnswerRe: DropShadowEffect Above & Below Pin
Richard Deeming30-Nov-22 0:43
mveRichard Deeming30-Nov-22 0:43 
GeneralRe: DropShadowEffect Above & Below Pin
Kevin Marois30-Nov-22 6:32
professionalKevin Marois30-Nov-22 6:32 
GeneralRe: DropShadowEffect Above & Below Pin
Richard Deeming30-Nov-22 22:30
mveRichard Deeming30-Nov-22 22:30 
QuestionWhat's Wrong With This Style? Pin
Kevin Marois21-Nov-22 10:57
professionalKevin Marois21-Nov-22 10:57 
AnswerRe: What's Wrong With This Style? Pin
Gerry Schmitz21-Nov-22 11:17
mveGerry Schmitz21-Nov-22 11:17 
GeneralRe: What's Wrong With This Style? Pin
Kevin Marois21-Nov-22 12:44
professionalKevin Marois21-Nov-22 12:44 
QuestionStyle Question Pin
Kevin Marois20-Nov-22 11:30
professionalKevin Marois20-Nov-22 11:30 
AnswerRe: Style Question Pin
Richard Deeming20-Nov-22 23:37
mveRichard Deeming20-Nov-22 23:37 
GeneralRe: Style Question Pin
Kevin Marois21-Nov-22 9:05
professionalKevin Marois21-Nov-22 9:05 
Questionediting a WPF DataGrid Pin
pitwi13-Nov-22 10:08
pitwi13-Nov-22 10:08 
AnswerRe: editing a WPF DataGrid Pin
Gerry Schmitz15-Nov-22 8:38
mveGerry Schmitz15-Nov-22 8:38 

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.