[Updated] Attached Properties to UserControl
-
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"