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

  • Home
  • SEARCH
  • 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 3401212
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:59:16+00:00 2026-05-18T04:59:16+00:00

I’m trying to build a custom control for the Windows Phone 7 platform. So

  • 0

I’m trying to build a custom control for the Windows Phone 7 platform. So far I have the property defined and it works fine if it’s a simple type (like a string), but this ultimately needs to be an array. So I define my property like this:

    #region Keywords

    public string[] Keywords
    {
        get { return (string[])GetValue(KeywordsProperty); }
        set { SetValue(KeywordsProperty, value); }
    }

    public static readonly DependencyProperty KeywordsProperty =
        DependencyProperty.Register(
            "Keywords",
            typeof(string[]),
            typeof(MyType),
            new PropertyMetadata(null));

    #endregion Keywords

Now I want top support binding and XAML, but I can’t figure out how to set the property via XAML. Is there a better way to do this?

  • 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-18T04:59:16+00:00Added an answer on May 18, 2026 at 4:59 am

    Using a string[] in XAML

    You can instantiate your Keywords property in XAML as follows:

    <MyControl ...>
      <MyControl.Keywords>
        <sys:String>Happy</sys:String>
        <sys:String>Sad</sys:String>
        <sys:String>Angry</sys:String>
      </MyControl.Keywords>
    </MyControl>
    

    Note: This assumes the namespace declaration

    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    

    You can bind an ItemsControl to your Keywords as follows:

    <ListBox ItemsSource="{Binding Keywords}" ...>
    

    In this example each Keyword will show up as a separate ListBox item.

    Using a string[] for this purpose in WPF is perfectly kosher, as long as the array members are guaranteed to never change (you can replace the array with a new one, but not change its elements). This is because an array has no change notification mechanism.

    If your elements might change

    If the elements of your Keywords array might change, you should use an ObservableCollection instead of an array. Make it a read-only CLR property (not a dependency property) and initialize it in your constructor. Then you can populate it in XAML exactly as shown above, but any changes to the collection will be immediately reflected in your UI.

    I actually use my own collection classes for this which have a lot more functionality than ObservableCollection. It doesn’t matter what collection class you use as long as it implements INotifyCollectionChanged.

    Making the list updatable using pure XAML

    You cannot update your Keywords array using a Binding. If you need to update the Keywords and want to do it entirely using XAML and bindings, you’ll need to create a Keyword object class that holds the string, then put them in an ObservableCollection or similar.

    IValueConverter

    Another approach is to use a IValueConverter to convert a comma-separated list of strings to and from a string[], like this:

    <TextBox Text="{Binding Keywords,
                            Converter={x:Static my:CommaSeparatedConverter.Instance}}" />
    

    where the converter would be:

    public class CommaSeparatedConverter : IValueConverter
    {
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
        return string.Join(",", (string[])value);
      }
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
        return ((string)value).Split(',');
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.