Silverlight Custom Control Binding

I'm trying to make a silverlight custom control that contains a busy indicator and a content presenter.  I want the busy indicator "IsBusy" property to be bound to a property in the datacontext of the content control content.

I've attached my control, generic.xaml and part of the view service that attaches they viewmodel to the view.  

I want to bind the "IsBusy" property to a property called "IsReady" in the viewmodel.  I will need to use a converter as well to inverse the IsReady property.  

How would I do this?






// My custom control 
public class BusyControl : ContentControl
    {
        public BusyControl()
        {
            this.DefaultStyleKey = typeof(BusyControl);
        }

        public static readonly DependencyProperty IsBusyProperty = DependencyProperty.Register("IsBusy", typeof(bool), typeof(BusyControl), new PropertyMetadata(null));

        public bool IsBusy
        {
            get { return (bool)GetValue(IsBusyProperty); }
            set { SetValue(IsBusyProperty, value); }
        }
    }

// Generic.xaml
<ResourceDictionary xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Sdi.Silverlight.Core">


    <Style TargetType="local:BusyControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:BusyControl">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <toolkit:BusyIndicator x:Name="PART_BusyIndicator" IsBusy="{TemplateBinding ?}">

                            <toolkit:BusyIndicator.BusyContentTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <TextBlock Text="Processing...Please Wait"></TextBlock>
                                    </StackPanel>
                                </DataTemplate>
                            </toolkit:BusyIndicator.BusyContentTemplate>

                            <ContentPresenter x:Name="PART_Content" Content="{TemplateBinding Content}"/>

                        </toolkit:BusyIndicator>

                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>



//In My View Service
public void ShowView(IViewModel viewModel, UserControl view, object parameters)
        {
            Guard.ArgumentNotNull(viewModel, "viewModel");
            Guard.ArgumentNotNull(view, "view");
            
            _currentViewModel = viewModel;
            _currentView = view;

            this.CurrentShell.CurrentContentContainer.Content = view;
                   
            viewModel.Load(parameters);
          
            view.DataContext = viewModel;

            OnPropertyChanged("CurrentView");
            OnPropertyChanged("CurrentViewModel");
        }

Open in new window

weimhaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
http://www.silverlightshow.net/items/Model-View-ViewModel-in-Silverlight.aspx

About 1/4 the way down:

"Second we should declare a new instance of the view model in the resources of the view:"


Create an instance of your view model in your control.  Then bind using the datacontext of the VM to get the value.

0
somanadhaCommented:
If I understand your question correctly,

You want to show/hide the control based on IsReady property in VM, right?

If so you don't need IsBusy property. Just bind the Visibility to IsReady with a BooleanToVisibility converter.

Let me know if my answer make sense to you or not?
0
weimhaAuthor Commented:
ged325:  I don't know the specific implementation of the view model to create.  This control needs to work with all of our view models that implement our IViewModel interface, which contains a property called IsReady.

somandha:   I don't want to show/hide the control.  I want to use the busy indicator.  Some of our viewmodels take a little while to load and we need to show our users that something is happening.

I'm trying to insert this into our current view/viewmodel/view service configuration.
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

somanadhaCommented:
Well, if you want to show this control before view model is built, you can use the 'fallbackvalue' to true for the visibility and once VM is built it will update with 'false'. How about that?
0
weimhaAuthor Commented:
somandha, I don't know what the "fallbackvalue" is.  Tthe user's want to see the busy indicator.
0
somanadhaCommented:
Let me backup. Are you using SL4? I believe fallback is supported in SL4 only.

here is how you do it:

<TextBlock   Text="{Binding FirstName, FallbackValue=N/A}" />  

If the FirstName is not built yet, then N/A will be taken as the default (fallback) value
0
weimhaAuthor Commented:
somandha, the users want the busy indicator.  They like it.

I've tried setting the binding like this and it just doesn't show the busy indicator

 <toolkit:BusyIndicator x:Name="PART_BusyIndicator" IsBusy="{Binding Path=DataContext.IsReady}" >


0
theHollowCommented:
Hello
From what I can see, your code is good.
The only thing missing is that your control should also inherit the "INotifyPropertyChanged" interface.

public class BusyControl : ContentControl, INotifyPropertyChanged
{
    // your stuff
}

Open in new window


This will make you implement this code:

public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(String aPropertyName)
{
   if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(aPropertyName));
}

Open in new window


Then you should fire the "PropertyChanged"-event in your "IsBusy"-property.
Like this:

public bool IsBusy
{
    get { return (bool)GetValue(IsBusyProperty); }
    set
    {
        SetValue(IsBusyProperty, value);
        NotifyPropertyChanged("IsBusy");
    }
}

Open in new window


And your XAML would look like this:
<toolkit:BusyIndicator IsBusy="{TemplateBinding IsBusy}">

Open in new window

(no need for x:Name :-) )

By doing this, the UI will know when the "IsBusy"-property changed, and then update the binding.

Hope that helps :-)
0
weimhaAuthor Commented:
This project has been put on hold.  Thanks for you help.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
weimhaAuthor Commented:
This project has been put on hold.  Thanks for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.