Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1082799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:21:03+00:00 2026-05-16T22:21:03+00:00

I have a custom WPF Line, that I whant to change the Style when

  • 0

I have a custom WPF Line, that I whant to change the Style when selecting with mouse.

I tried to use triggers(see code bellow), but this does not work.

Where is the problem? The IsSelected property is changed, but the StrokeThickness does not change at all… (

XAML:

<UserControl.Resources>
 <Style TargetType="stops:MyLine" 
  x:Key="MyLineStyleKey" x:Name="MyLineStyleName">

  <Style.Triggers>
   <DataTrigger Binding="{Binding Path=IsSelected}" Value="true">
    <Setter Property="StrokeThickness" Value="10" />
   </DataTrigger>
  </Style.Triggers>
 </Style>
</UserControl.Resources>

<Canvas...
...
<my:MyLine Style="{StaticResource MyLineStyleKey}" x:Name="myLine1" 
 Stroke="Black" X11="10" X22="100" Y11="10" Y22="100"/>

Code:

class MyLine : Shape
{
    public static readonly DependencyProperty X11Property;
    public static readonly DependencyProperty X22Property;
    public static readonly DependencyProperty Y11Property;
    public static readonly DependencyProperty Y22Property;
    public static readonly DependencyProperty IsSelectedProperty;

    static MyLine()
    {
        X11Property = DependencyProperty.Register("X11", typeof(double), typeof(MyLine), new FrameworkPropertyMetadata(double.NaN, FrameworkPropertyMetadataOptions.AffectsRender));
        X22Property = DependencyProperty.Register("X22", typeof(double), typeof(MyLine), new FrameworkPropertyMetadata(double.NaN, FrameworkPropertyMetadataOptions.AffectsRender));
        Y11Property = DependencyProperty.Register("Y11", typeof(double), typeof(MyLine), new FrameworkPropertyMetadata(double.NaN, FrameworkPropertyMetadataOptions.AffectsRender));
        Y22Property = DependencyProperty.Register("Y22", typeof(double), typeof(MyLine), new FrameworkPropertyMetadata(double.NaN, FrameworkPropertyMetadataOptions.AffectsRender));
        IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(MyLine), new FrameworkPropertyMetadata(false));
    }

    public MyLine()
        : base()
    {
        this.PreviewMouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(DirectionLine_PreviewMouseLeftButtonDown);
    }

    void DirectionLine_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        this.IsSelected = !this.IsSelected;
    }

    [TypeConverter(typeof(LengthConverter))]
    public double X11 { get { return (double)GetValue(X11Property); } set { SetValue(X11Property, value); } }
    [TypeConverter(typeof(LengthConverter))]
    public double X22 { get { return (double)GetValue(X22Property); } set { SetValue(X22Property, value); } }
    [TypeConverter(typeof(LengthConverter))]
    public double Y11 { get { return (double)GetValue(Y11Property); } set { SetValue(Y11Property, value); } }
    [TypeConverter(typeof(LengthConverter))]
    public double Y22 { get { return (double)GetValue(Y22Property); } set { SetValue(Y22Property, value); } }
    [TypeConverter(typeof(BooleanConverter))]
    public bool IsSelected { get { return (bool)GetValue(IsSelectedProperty); } set { SetValue(IsSelectedProperty, value); } }


    protected override System.Windows.Media.Geometry DefiningGeometry
    {
        get
        {
            var geometryGroup = new GeometryGroup();
            geometryGroup.Children.Add(new LineGeometry(new Point(X11, Y11), new Point(X22, Y22)));
            return geometryGroup;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:21:04+00:00Added an answer on May 16, 2026 at 10:21 pm

    Try using “<Trigger Property="IsSelected" ...>” instead of a DataTrigger. DataTrigger works on the DataContext for a FrameworkElement. In your case, this means that it will look for the IsSelected property of the control’s DataContext. Trigger, on the other hand, works directly on the FrameworkElement itself, and looks for Dependency Properties of the FrameworkElement.

    <UserControl x:Class="StackOverflow.MyUserControl"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:local="clr-namespace:StackOverflow">
        <UserControl.Resources>
            <Style TargetType="local:MyLine" 
      x:Key="MyLineStyleKey" x:Name="MyLineStyleName">
    
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="StrokeThickness" Value="10" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </UserControl.Resources>
        <Canvas>
            <local:MyLine Style="{StaticResource MyLineStyleKey}" x:Name="myLine1" 
    Stroke="Black" X11="10" X22="100" Y11="10" Y22="100"/>
        </Canvas>
    </UserControl>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In WPF, I have a custom control that inherits from TreeView. The code is
I have a custom WPF Line and its style. UserControl Resources: <!-- Framework properties
I have a WPF application where I'd like to create a custom pop-up that
I have a wpf app that needs to communicate(exchange data) with a custom designed
I have a custom control in WPF that simply holds a combo box (however
I have a custom WPF user control called a TimeoutPanel that I am trying
I have a C# custom WPF control. I have properties on the control that
I have a custom wpf control to do an arbitrary reverse image warp, that
I have a WPF custom control that sometimes takes a while to render in
I have a custom template for my WPF windows ComboBoxes, which display items that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.