Dependency property either shows up blank or some kind of exception RRS feed

  • Question

  • I'm trying to set up a way for me to put strings into a user control for an appbar and have them show up int squares. Pretty much the same thing as using a listview or gridview, but I want them to stretch out so that they go to the edge of the screen. I'm manually putting them into a grid and setting the grid's columns based on that. I need some help, I think, with what to set the "new PropertyMetaData" part to. Thanks!

    Here's what I've got:

    public List<string> NavItems
                get { return (List<string>)GetValue(NavItemsProperty); }
                set { SetValue(NavItemsProperty, value); }
            // Using a DependencyProperty as the backing store for NavItems.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty NavItemsProperty =
                DependencyProperty.Register("NavItems", typeof(List<string>), typeof(NavigationBar), new PropertyMetadata(null, MakeItems));
            private static void MakeItems(DependencyObject d, DependencyPropertyChangedEventArgs e)
                // iterate through each item in the list,
                // make a column def,
                // apply item to that def
                NavigationBar nb = (NavigationBar)d;
                for (int i = 0; i < nb.NavItems.Count; i++)
                    ListViewItem lvItem = new ListViewItem()
                        Background = Application.Current.Resources["PhoneAccentBrush"] as SolidColorBrush
                    Binding b = new Binding()
                         ElementName = lvItem.Name,
                         Path = new PropertyPath("ActualWidth")
                    Border border = new Border()
                        HorizontalAlignment = HorizontalAlignment.Stretch,
                        VerticalAlignment = VerticalAlignment.Stretch,
                    border.SetBinding(WidthProperty, b);
                    TextBlock tb = new TextBlock() 
                        Style = App.Current.Resources["SubheaderTextStyle"] as Style,
                        Text = nb.NavItems[i],
                        FontWeight = FontWeights.Medium,
                        Margin = new Thickness(0,6,0,12),
                        HorizontalAlignment = HorizontalAlignment.Center,
                        VerticalAlignment = VerticalAlignment.Center
                    lvItem.Content = border;
                    border.Child = tb;
                    lvItem.Tapped += nb.lvItem_Tapped;
                    nb.LayoutRoot.ColumnDefinitions.Add(new ColumnDefinition());
                    Grid.SetColumn(lvItem, i);
            public NavigationBar()

                <AppBar:NavigationBar HorizontalAlignment="Stretch">

    Michael DiLeo

    Saturday, November 9, 2013 5:09 PM

All replies

  • Michael - do you mind posting an app so I can test this out?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, November 11, 2013 8:13 PM
  • Sure, though, I did find a work around. I wasn't able to set the list from XAML, so I changed up my code to use a regular property, set the strings as a private string[], and used an enumeration to identify the one that I didn't want to add. However, I would still like to know how to use a list as a dependency property or at least be able to set it in XAML. If I could do that, I could use something like a navigation item class that has text and I uri and use the navigation bar in multiple apps or something.

    Here's the link to a project that I made off of the one I was working on. Something weird happened, though. I set it up the same way I had it when I was getting my results to show up in the UI at design time, but this one is saying that there's an error x:String and that it can't be found. Thanks for your help.


    Michael DiLeo

    Monday, November 11, 2013 9:50 PM