Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. [Updated] Attached Properties to UserControl

[Updated] Attached Properties to UserControl

Scheduled Pinned Locked Moved WPF
wpf
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mc_Topaz
    wrote on last edited by
    #1

    I have a UserControl which a bunch of Controls. In the UserControl there is TextBlock displaying a text as a title. The title is implemented as a DepedencyProperty in my UserControl and I can easily set the TextBlock's value in the MainWindow's XAML code. Like this:

    Notice my custom Title property. I also have a button (Button1) in the UserControl with some properties. I want to specify the button's properties in the MainWindow XAML-code. But I don't manage to gain access to the buttons properties in the XAML-code if I use a similar Dependency property. I want this:

    As you see I'm interesting to group the Button1's properties and be able to set its properties in the MainWindow's XAML-code. But with no success :sigh: Here is the code for the MyUserControl of what I have tried:

    public class MyButton: FramworkElement
    {
    public ICommand Command { get; set; }
    public string Header { get; set; }
    }

    public class MyUserControl : UserControl
    {
    public static readonly DependencyProperty Button1Property = DependencyProperty.RegisterAttached
    (
    "Button1",
    typeof(MyButton),
    typeof(MyUserControl),
    new FrameworkPropertyMetadata(false, new PropertyChangedCallback(Button1Changed))
    );

    public MyButton Button1
    {
        get { return (MyButton)GetValue(Button1Property); }
        set { SetValue(Button1Property, value);  }
    }
    
    public static void Button1Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // ToDo
    }
    

    }

    I found this weird because this kinda works when enclosing a control in a DockPanel. DockPanel.Dock="Left"

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups