Click here to Skip to main content
15,883,938 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: fast directory infos for a lot of files Pin
Richard Deeming6-Dec-22 2:51
mveRichard Deeming6-Dec-22 2:51 
GeneralRe: fast directory infos for a lot of files Pin
pitwi6-Dec-22 6:56
pitwi6-Dec-22 6:56 
QuestionCustom Control Styling Pin
Kevin Marois2-Dec-22 8:58
professionalKevin Marois2-Dec-22 8:58 
AnswerRe: Custom Control Styling Pin
Richard Deeming5-Dec-22 0:27
mveRichard Deeming5-Dec-22 0:27 
GeneralRe: Custom Control Styling Pin
Kevin Marois5-Dec-22 12:15
professionalKevin Marois5-Dec-22 12:15 
GeneralRe: Custom Control Styling Pin
Richard Deeming5-Dec-22 21:57
mveRichard Deeming5-Dec-22 21:57 
GeneralRe: Custom Control Styling Pin
Kevin Marois5-Dec-22 13:57
professionalKevin Marois5-Dec-22 13:57 
GeneralRe: Custom Control Styling Pin
Kevin Marois5-Dec-22 14:10
professionalKevin Marois5-Dec-22 14:10 
I beleive I finally have it working! This was mainly about me learning how to create Custome Controls. I appreciate all your help. I learned a lot.

Here's the finiished product!
Generic.xaml
<Style TargetType="{x:Type local:MaroisHyperlink}">

    <Setter Property="Template">

        <Setter.Value>

            <ControlTemplate>

                <TextBlock>

                    <Hyperlink x:Name="hyperLink" 
                                Foreground="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}"
                                TextDecorations="{Binding TextDecorations, RelativeSource={RelativeSource TemplatedParent}}">

                        <TextBlock Text="{Binding LinkText, 
                                    RelativeSource={RelativeSource TemplatedParent}}"/>

                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource FindAncestor, 
                                                                    AncestorType=Control}, Path=LinkClickedCommand}"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>

                    </Hyperlink>

                </TextBlock>

            <ControlTemplate.Triggers>

                <Trigger Property="IsMouseOver" Value="true">
                    <Setter TargetName="hyperLink" Property="TextDecorations" Value="{Binding TextDecorations, RelativeSource={RelativeSource TemplatedParent}}"/>
                    <Setter TargetName="hyperLink" Property="Foreground" Value="{Binding HoverBrush, RelativeSource={RelativeSource TemplatedParent}}"/>
                </Trigger>

            </ControlTemplate.Triggers>

            </ControlTemplate>

        </Setter.Value>

    </Setter>

</Style>
Code Behind
C#
public class MaroisHyperlink : ControlBase
{
    #region Routed Events
    #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()
    {
        RoutedEventArgs args = new RoutedEventArgs(LinkClickedEvent);
        RaiseEvent(args);
    }
    #endregion
    #endregion

    #region DP's
    #region DP TextDecorations
    public static readonly DependencyProperty TextDecorationsProperty =
        Inline.TextDecorationsProperty.AddOwner(typeof(MaroisHyperlink));

    public TextDecorationCollection TextDecorations
    {
        get { return (TextDecorationCollection)GetValue(TextDecorationsProperty); }
        set { SetValue(TextDecorationsProperty, value); }
    }
    #endregion

    #region DP HoverBrush
    public static readonly DependencyProperty HoverBrushProperty =
                DependencyProperty.Register("HoverBrush",
                typeof(SolidColorBrush),
                typeof(MaroisHyperlink),
                new PropertyMetadata(new SolidColorBrush(Colors.Green)));

    public SolidColorBrush HoverBrush
    {
        get { return (SolidColorBrush)GetValue(HoverBrushProperty); }
        set { SetValue(HoverBrushProperty, value); }
    }
    #endregion

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

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

    #region Commands
    private ICommand? _LinkClickedCommand;
    public ICommand LinkClickedCommand
    {
        get
        {
            if (_LinkClickedCommand == null)
                _LinkClickedCommand = new RelayCommand(LinkClickedExecuted);
            return _LinkClickedCommand;
        }
    }
    #endregion

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

    #region Private Methods
    private void LinkClickedExecuted()
    {
        RaiseLinkClickedEvent();
    }
    #endregion
}
Usage, with overriding style
XML
<Window x:Class="Marois.Framework.Core.WPF.Controls.Demo.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:i="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:marois="clr-namespace:Marois.Framework.Core.WPF.Controls;assembly=Marois.Framework.Core.WPF.Controls"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="Marois Controls Demo" 
        Height="450" 
        Width="800">

    <Window.Resources>

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

            <Setter Property="HoverBrush" Value="OliveDrab"/>
            <Setter Property="Foreground" Value="Cyan"/>
            <Setter Property="FontSize" Value="30"/>

            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Foreground" Value="BlanchedAlmond" />
                    <Setter Property="TextDecorations" Value="Underline" />
                </Trigger>

                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Foreground" Value="LightGray" />
                    <Setter Property="TextDecorations" Value="None" />
                </Trigger>

            </Style.Triggers>

        </Style>

    </Window.Resources>

    <Grid>

        <StackPanel Orientation="Vertical">

            <CheckBox x:Name="checkBox"
                      Content="Enabled"
                      IsChecked="True"
                      Margin="5"/>

            <marois:MaroisHyperlink x:Name="link" 
                                    Margin="5"
                                    IsEnabled="{Binding ElementName=checkBox, Path=IsChecked}"
                                    HorizontalAlignment="Left"
                                    VerticalAlignment="Top">

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

            </marois:MaroisHyperlink>

        </StackPanel>

    </Grid>

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

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 
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 

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.