Chris W.
Chris W.

Reputation: 23290

Disappearing UserControl after build

The Issue: I have a newly made UserControl with a couple telerik controls in a parent Grid to be able to use it throughout the solution. Sounds simple enough right? I created the UserControl, let's call the Class My.Project.Controls.Tool, which I then tried to call to another View with the namespace xmlns:Controls="clr-namespace:My.Project.Controls;assembly=My.Project.GlobalUserControlDump" and then set it in the view via easily selected from the handy dandy intellisense.

This does as is expected, my UserControl appears on the separate view in the designer just fine. So I take the next normal step and build it....as soon as the build completes (which it does just fine with no errors reported as expected) the little bugger disappears! The xaml is still on the View of course, but it's disappeared from the designer AND it doesnt appear on the built solution?

Confused I go back, make a quick change to the UserControl and it appears back in the designer. Ok, I think it must be some fluke so I build it again....and again it disappears from the designer AND the built solution?

Now I can continue to reliably reproduce this scenario. Make a little change to the UserControl, it re-appears in the designer.....then build it and it disappears again!

Could someone pretty please shed some light on this quandry? Running in SL (both in and out of browser but built in browser) with Caliburn Micro. Any insight to this mystery is of course greatly appreciated, hoping another pair of eyes can catch my folly. Cheers!

For Clarification, this is what sits in the user control that directly related to a previous question.

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    mc:Ignorable="d"
    x:Class="My.Project.Controls.DatePicker">

    <Grid Width="90">
           <telerik:RadDateTimePicker 
                                      InputMode="DatePicker" 
                                      SelectedDate="{Binding SelectedDate, Mode=TwoWay}"/>
           <telerik:RadMaskedDateTimeInput
                                           IsClearButtonVisible="False"
                                           FormatString="{}{0:M/d/yy}"
                                           SelectionOnFocus="SelectAll"
                                           Value="{Binding SelectedDate, Mode=TwoWay}"/>
    </Grid>
</UserControl>

Which I would then invoke directly on a view like (It would sit in the GlobalUserControlDump project as the namespace shows) and once the namespace is added to the View, it shows up fine in the intellisense as expected;

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro" 
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    x:Class="My.Project.Views.RandomView" 
    xmlns:Controls="clr-namespace:My.Project.Controls;assembly=My.Project.GlobalUserControlDump"
    mc:Ignorable="d">

    <Grid>
            <Controls:DatePicker />
    </Grid>
 </UserControl>

Then I'm exposing the property I need via;

namespace My
{
    public partial class DatePicker : UserControl
    {
        public static readonly DependencyProperty SelectedDateProperty =
            DependencyProperty.Register("SelectedDate", typeof(DateTime), typeof(DatePicker), new PropertyMetadata(null));

        public DatePicker()
        {
            // Required to initialize variables
            DataContext = this;
        }

        public DateTime SelectedDate
        {
            get { return (DateTime)GetValue(SelectedDateProperty); }
            set { SetValue(SelectedDateProperty, value); }
        }
    }
}

Thanks for taking a look, has me still currently stumped.

Upvotes: 1

Views: 1388

Answers (2)

Derek Beattie
Derek Beattie

Reputation: 9478

I believe your namespace in the code you posted is incorrect.

By having new PropertyMetadata(null)); a property changed callback isn't being registered. Without that I believe binding won't work. What you want to do is bind to the property on your usercontrol and when the bound value changes, you want to set the value on the RadDateTimePicker contained in your control.

xaml:

    <Grid Width="90">
      <telerik:RadDateTimePicker x:Name="MyRadDateTimePicker" InputMode="DatePicker" />
    <telerik:RadMaskedDateTimeInput x:Name="MyRadMaskedDateTimeInput"
                                    IsClearButtonVisible="False"
                                    FormatString="{}{0:M/d/yy}"
                                    SelectionOnFocus="SelectAll" />
    </Grid>
</UserControl>

code behind:

 using System;
using System.Windows;

namespace SO
{
    public partial class MyDatePicker
    {
        public MyDatePicker()
        {
            InitializeComponent();
        }

        public const string SelectedDatePropertyName = "SelectedDate";

        public DateTime SelectedDate
        {
            get { return (DateTime)GetValue(SelectedDateProperty); }
            set { SetValue(SelectedDateProperty, value); }
        }

        public static readonly DependencyProperty SelectedDateProperty = DependencyProperty.Register(
            SelectedDatePropertyName,
            typeof(DateTime),
            typeof(MyDatePicker),
            new PropertyMetadata(DateTime.Now, OnSelectedDatePropertyChanged));

        private static void OnSelectedDatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((MyDatePicker)d).MyRadDateTimePicker.SelectedDate = (DateTime) e.NewValue;
            ((MyDatePicker)d).MyRadMaskedDateTimeInput.Value = (DateTime)e.NewValue;
        }
    }
}

Upvotes: 1

Aleksandr Dubinsky
Aleksandr Dubinsky

Reputation: 23515

You are missing a call to InitializeComponent() in you UserControl's constructor.

Upvotes: 5

Related Questions