none
UserControl = DataObject + UI??? RRS feed

  • Question

  • I'm designing some UserControls and have some pretty basic questions:

     

    0. I'm putting several UserControls into a library, using the VS2008 WPF UserControl Library template. How do I refer in XAML to a string resource in the Properties folder of the UserControl library project?

    ( Note that I'm referring to a plain ol' vanilla-style string table set up in the Properties folder...not the UserControl.Resources type resources.)

     

     

    1. Should user controls contain data fields, ie separate variables declared in the codeBehind? The data can after all be stored in the controls which display the data, and it seems pointless to make a user control a mixture of business object and UI element...

     

    I have an AddressLabel, which shows Address data, each separate field labelled:

    Street: 17 Main Street

    Apartment: 18

    ...

    Country: USA

    I can name TextBlock labels in xaml and load globalizeable label text from the resource file to specify the label text associated with each different field - "Street:" in english, "Rue: " in French, etc

    eg

    Code Block

    <UserControl...>

     

    <StackPanel Orientation="Horizontal">

    <TextBlock Name="address1Label" />

    <TextBlock Name="address1Value" />

    </StackPanel>

    </UserControl>

     

    //and then have properties in the codebehind:

     

    public string Address1Label

    {

    get { return address1Label.Text;}

    set { address1Label.Text = value;OnPropertyChanged("Address1Label"); }

    }

    public string Address1Value

    {

    get { return address1Value.Text;}

    set { address1Value.Text = value;OnPropertyChanged("Address1Value"); }

    }

     

     

     

    Alternatively, I could see the codebehind containing a bunch of fields which have the data, and the XAML elements binding to the data fields there:

    Code Block

     

    <UserControl...>

     

    <StackPanel Orientation="Horizontal">

    <TextBlock Text="{Bind to Address1Label}" />

    <TextBlock Text="{Bind to Address1Value}" />

    </StackPanel>

    </UserControl>

    // use regular .Net properties

     

    private string addressLabelText

    public string Address1Label

    {

    get { return addressLabelText;}

    set { addressLabelText = value;OnPropertyChanged("Address1Label"); }

    }

    private string addressValue

    public string Address1Value

    {

    get { return addressValue;}

    set { addressValue = value;OnPropertyChanged("Address1"); }

    }

     

     

     

    Which, if either, is The Correct Way?

    Monday, December 3, 2007 2:06 AM

Answers

  • Either will work...

     

    a) The first way is slightly simpler to code up. You have called OnPropertyChanged, but in fact you probably dont need this (or indeed INotifiyPropertyChanged at all).

     

    b) The second way is more "lookless", in that it more cleanly separates the public interface of the control from its implemenation. If you want to change the xaml later, you can be more confident of not breaking users code.

     

    However...

     

    c) There is a third way, which is probably the best way. Implement these properties as Dependency Properties. This has the major advantage that users of your control can then Bind onto your properties. Only DependencyPropertis can be the target of a data binding. Your colleagues will be very happy.

     

    and when you do it done this...

     

    d) You are a stones throw from implementing your control as a custom control. This is the advantage that users of your control can re-template it themselves. When your designer throws a strop and wants a pink radial gradient on the control, you'll be able to tell him to do himself (Thats my personal take on good designer-developer integration anyway). You will be revered amongst your peers.

     

    Regards,

    Nick.

     

    Monday, December 3, 2007 8:21 AM
  •  doctorg wrote:

    0. I'm putting several UserControls into a library, using the VS2008 WPF UserControl Library template. How do I refer in XAML to a string resource in the Properties folder of the UserControl library project?

    ( Note that I'm referring to a plain ol' vanilla-style string table set up in the Properties folder...not the UserControl.Resources type resources



    The following snippet shows how to make this work:
    Code Block

    <StackPanel xmlns:cc="clr-namespace:MyProjectNamespace.Properties"> 

    <Label Content="{Binding Source={x:Static cc:Resources.MyString}}"/> 

    </StackPanel>


    "MyProjectNamespace" should be the namespace of your project. and MyString should be a public static property, because StaticExtension can only refer to public static members.

    Hope this helps
    Thursday, December 6, 2007 5:45 AM

All replies

  • Either will work...

     

    a) The first way is slightly simpler to code up. You have called OnPropertyChanged, but in fact you probably dont need this (or indeed INotifiyPropertyChanged at all).

     

    b) The second way is more "lookless", in that it more cleanly separates the public interface of the control from its implemenation. If you want to change the xaml later, you can be more confident of not breaking users code.

     

    However...

     

    c) There is a third way, which is probably the best way. Implement these properties as Dependency Properties. This has the major advantage that users of your control can then Bind onto your properties. Only DependencyPropertis can be the target of a data binding. Your colleagues will be very happy.

     

    and when you do it done this...

     

    d) You are a stones throw from implementing your control as a custom control. This is the advantage that users of your control can re-template it themselves. When your designer throws a strop and wants a pink radial gradient on the control, you'll be able to tell him to do himself (Thats my personal take on good designer-developer integration anyway). You will be revered amongst your peers.

     

    Regards,

    Nick.

     

    Monday, December 3, 2007 8:21 AM
  •  doctorg wrote:

    0. I'm putting several UserControls into a library, using the VS2008 WPF UserControl Library template. How do I refer in XAML to a string resource in the Properties folder of the UserControl library project?

    ( Note that I'm referring to a plain ol' vanilla-style string table set up in the Properties folder...not the UserControl.Resources type resources



    The following snippet shows how to make this work:
    Code Block

    <StackPanel xmlns:cc="clr-namespace:MyProjectNamespace.Properties"> 

    <Label Content="{Binding Source={x:Static cc:Resources.MyString}}"/> 

    </StackPanel>


    "MyProjectNamespace" should be the namespace of your project. and MyString should be a public static property, because StaticExtension can only refer to public static members.

    Hope this helps
    Thursday, December 6, 2007 5:45 AM