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. WPF UserControl design question

WPF UserControl design question

Scheduled Pinned Locked Moved WPF
wpfquestioncsharpdesignhelp
4 Posts 2 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.
  • A Offline
    A Offline
    Alex Barry
    wrote on last edited by
    #1

    I am designing a WPF UserControl, and I'm not sure how to do part of it correctly. The main control to be consumed by the user will have three "sub-controls", that is controls that the main control composes to create the whole: public class MainControl : UserControl { public SubControl Sub1 {get; private set;} public SubControl Sub2 {get; private set;} public SubControl Sub3 {get; private set;} } public class SubControl : Canvas { // Can be any user control, I just happen to use a Canvas public int Property1 {get; set;} public string Property2 {get; set;} } What I would like to do is change the code so that the properties of the the SubControl's are editable in XAML. I'm aiming for something along the lines of: I have played around making the properties of SubControl DPs, as well as making each of the SubControl's DPs, but I keep getting error MC3057. The only way I have been able to get it to work is to add DPs to the MainControl for each of the properties of the SubControl, and bind them together. That's kind of ugly and scales poorly. Does anybody have any suggestions?

    L 1 Reply Last reply
    0
    • A Alex Barry

      I am designing a WPF UserControl, and I'm not sure how to do part of it correctly. The main control to be consumed by the user will have three "sub-controls", that is controls that the main control composes to create the whole: public class MainControl : UserControl { public SubControl Sub1 {get; private set;} public SubControl Sub2 {get; private set;} public SubControl Sub3 {get; private set;} } public class SubControl : Canvas { // Can be any user control, I just happen to use a Canvas public int Property1 {get; set;} public string Property2 {get; set;} } What I would like to do is change the code so that the properties of the the SubControl's are editable in XAML. I'm aiming for something along the lines of: I have played around making the properties of SubControl DPs, as well as making each of the SubControl's DPs, but I keep getting error MC3057. The only way I have been able to get it to work is to add DPs to the MainControl for each of the properties of the SubControl, and bind them together. That's kind of ugly and scales poorly. Does anybody have any suggestions?

      L Offline
      L Offline
      Leung Yat Chun
      wrote on last edited by
      #2

      The proper syntax :

      public class MainControl : UserControl {
      public SubControl Sub1 {get; set;}
      }

      <bbb:MainControl x:Name="abc">
      <bbb.MainControl.Sub1>
      <SubControl x:Name="Sub1" Property1="5" Property2="Hello" />
      </bbb.MainControl.Sub1>
      </bbb:MainControl>

      If you want to change it in run time you have to use Trigger. For more information, please read WPF: A Beginner's Guide - Part 1 of n[^] Regards Joseph Leung

      QuickZip

      A 1 Reply Last reply
      0
      • L Leung Yat Chun

        The proper syntax :

        public class MainControl : UserControl {
        public SubControl Sub1 {get; set;}
        }

        <bbb:MainControl x:Name="abc">
        <bbb.MainControl.Sub1>
        <SubControl x:Name="Sub1" Property1="5" Property2="Hello" />
        </bbb.MainControl.Sub1>
        </bbb:MainControl>

        If you want to change it in run time you have to use Trigger. For more information, please read WPF: A Beginner's Guide - Part 1 of n[^] Regards Joseph Leung

        QuickZip

        A Offline
        A Offline
        Alex Barry
        wrote on last edited by
        #3

        I was kind of hoping to avoid allowing a set on the SubControl properties. Allowing the consumer to change these properties would require unhooking and rehooking events, reinitializing the new property, etc. Maybe I'm just being lazy, but I was hoping that I could make the XAML manipulate the existing instances instead of creating new ones and hooking them up. As for updating properties at run time, I would probably use code behind to update the properties.

        L 1 Reply Last reply
        0
        • A Alex Barry

          I was kind of hoping to avoid allowing a set on the SubControl properties. Allowing the consumer to change these properties would require unhooking and rehooking events, reinitializing the new property, etc. Maybe I'm just being lazy, but I was hoping that I could make the XAML manipulate the existing instances instead of creating new ones and hooking them up. As for updating properties at run time, I would probably use code behind to update the properties.

          L Offline
          L Offline
          Leung Yat Chun
          wrote on last edited by
          #4

          This may work, but not too maintainable.

          <UserControl.Triggers>
          <EventTrigger RoutedEvent="FrameworkElement.Loaded">
          <BeginStoryboard>
          <Storyboard>
          <ObjectAnimationUsingKeyFrames BeginTime="0:0:0" Storyboard.TargetProperty="Background">
          <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="Red" />
          </ObjectAnimationUsingKeyFrames>
          </Storyboard>
          </BeginStoryboard>
          </EventTrigger>
          </UserControl.Triggers>

          If you going to make DependencyProperty, you can inherit from current properties

          public static readonly DependencyProperty OrientationProperty =
          VirtualWrapPanel.OrientationProperty.AddOwner(typeof(VirtualWrapPanelView));

          Then Bind them in Xaml. Regards Joseph Leung

          QuickZip

          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