locked
UserControl with TextBlock

    Question

  • Hello everyone

    I'm trying to make a UserControl with two textblock to simulate a shadow.
    I have created a page form and I added three usercontrols . At designe mode everything Works fine however, after running my app any error happen but, just the third text of the usercontrol appears.

    See below the code:

    UserControl XAML

    <UserControl
        x:Name="MyUserControl">
        <Grid>
            <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Caption, ElementName=MyUserControl, Mode=TwoWay}" VerticalAlignment="Top" FontFamily="Happy Hell" FontSize="40" Margin="2,2,0,0" Foreground="Black" Opacity="0.5"/>
            <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Caption, ElementName=MyUserControl, Mode=TwoWay}" VerticalAlignment="Top" FontFamily="Happy Hell" FontSize="40" Foreground="White"/>
        </Grid>
    </UserControl>
    UserControl C#
     
    namespace myapptest
    {
        public sealed partial class Label : UserControl
        {
            public static DependencyProperty CaptionProperty { private set; get; }
            
            public String Caption
            {
                set { SetValue(CaptionProperty, value); }
                get { return (String)GetValue(CaptionProperty); }
            }
    
            public Label()
            {
                this.InitializeComponent();
                CaptionProperty = DependencyProperty.Register("Caption", typeof(string),
                typeof(Label), new PropertyMetadata("", OnTextChanged));
            }
    
            static void OnTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
            {
                (obj as Label).OnTextChanged(args);
            }
    
            void OnTextChanged(DependencyPropertyChangedEventArgs args)
            {
            }
        }
    }

    XAML MainPage

    <Grid>
        <local:Label x:Name="label1" HorizontalAlignment="Left" Margin="876,69,0,0" VerticalAlignment="Top" Caption="Address"/>
        <local:Label x:Name="label2" HorizontalAlignment="Left" Margin="870,153,0,0" VerticalAlignment="Top" Caption="Name"/>
        <local:Label x:Name="label2" HorizontalAlignment="Left" Margin="876,109,0,0" VerticalAlignment="Top" Caption="Birth date"/>
    </Grid>
    


    Thursday, August 21, 2014 4:38 AM

Answers

  • Your implementation of the dependency property in the UserControl is wrong. It should be defined like this:

        public sealed partial class Label : UserControl
        {
    
            public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register(
                "Caption", typeof(String),
                typeof(Label), new PropertyMetadata("", OnTextChanged)
                );
    
            public String Caption
            {
                set { SetValue(CaptionProperty, value); }
                get { return (String)GetValue(CaptionProperty); }
            }
    
            public Label()
            {
                this.InitializeComponent();
            }
    
            static void OnTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
            {
                (obj as Label).OnTextChanged(args);
            }
    
            void OnTextChanged(DependencyPropertyChangedEventArgs args)
            {
            }
        }
    

    Please refer to the following page for more information about dependency properties in Windows Store Apps: http://msdn.microsoft.com/en-us/library/hh700353.aspx

    Thursday, August 21, 2014 9:30 AM