locked
A stupid question: How to set the property's property of an Xaml element? RRS feed

  • Question

  • Hi, guys, long time no see! Recently I'm busy creating a custom Chart control using UserControl. Everything is ok until I was stucked on a stupid problem--I just can't set the property of the Axis of the my Chart. Here is my code:

    [MainPage.xaml]

    <Page
        x:Class="App28.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App28"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <local:MyChart >
                <!--this won't work-->
                <local:MyChart.Axis  SomeProperty="0"/>
    
                <!--this won't work either-->
                <local:MyChart.Axis2  SomeProperty="0" />
            </local:MyChart>
        </Grid>
    </Page>

    [MainPage.xaml.cs]

    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    
    
    namespace App28
    {
        public class MyChart: UserControl
        {
            public BottomAxis Axis
            {
                get { return (BottomAxis)GetValue(AxisProperty); }
                set { SetValue(AxisProperty, value); }
            }
            public static readonly DependencyProperty AxisProperty =
                DependencyProperty.Register("Axis", typeof(BottomAxis), typeof(MyChart), new PropertyMetadata(null));
    
            public BottomAxis Axis2 { get; set; }
        }
    
        public class BottomAxis : FrameworkElement
        {
            public int SomeProperty { get; set; }
    
        }
    
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
        }
    }

    btw: When would WinRT supply a standard Chart control? It's so painful to write one from scratch. And it's a small project, I can not afford a third-party control collection.


    Monday, December 16, 2013 3:34 PM

Answers

All replies

  • I moved this to the correct forum for you. The UI design forum is for discussing how the app should look, not how to implement the app. We cannot comment on future features, but I believe there are free chart controls available.

    Monday, December 16, 2013 4:07 PM
    Moderator
  • Hi, Rob,  thanks for your kindness! I didn't realize I am in UI design forum.

    Would you like to shed some light on my stupidest question? Really stuck here. 

    Best regards!

    Monday, December 16, 2013 4:25 PM
  • I'm not where I can look at it now. I moved it to where somebody else can help.

    --Rob

    Monday, December 16, 2013 4:32 PM
    Moderator
  • Hi Silverbird,

    you need to explicitly create a BottomAxis-object:

      <local:MyChart>
                <local:MyChart.Axis>
                    <local:BottomAxis SomeProperty="5"/>
                </local:MyChart.Axis>
                <local:MyChart.Axis2>
                    <local:BottomAxis SomeProperty="5"/>
                </local:MyChart.Axis2>
            </local:MyChart>

    You could also create a TypeConverter-subclass for the BottomAxis-Type that would allow you setting the Axis-Property like that:

    <local:MyChart Axis="5"/>

    @Rob: Great teamwork. ;-)


    Thomas Claudius Huber

    "If you can't make your app run faster, make it at least look & feel extremly fast"

    My latest app: The "Womanizer" :-)
    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook


    Monday, December 16, 2013 4:45 PM
  • Hi, Thomas, thanks for the quick reply.

    I've tested the first solution, and it works great.

    Many thanks!

    Monday, December 16, 2013 5:20 PM