Solved

Silverlight Custom Control Binding

Posted on 2011-09-15
10
893 Views
Last Modified: 2013-11-12
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

0
Comment
Question by:weimha
10 Comments
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 167 total points
ID: 36544133
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
 

Assisted Solution

by:somanadha
somanadha earned 167 total points
ID: 36548324
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
 

Author Comment

by:weimha
ID: 36548780
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Expert Comment

by:somanadha
ID: 36548850
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
 

Author Comment

by:weimha
ID: 36549121
somandha, I don't know what the "fallbackvalue" is.  Tthe user's want to see the busy indicator.
0
 

Expert Comment

by:somanadha
ID: 36549553
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
 

Author Comment

by:weimha
ID: 36566746
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
 
LVL 4

Assisted Solution

by:theHollow
theHollow earned 166 total points
ID: 36579627
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
 

Accepted Solution

by:
weimha earned 0 total points
ID: 37177786
This project has been put on hold.  Thanks for you help.
0
 

Author Closing Comment

by:weimha
ID: 37196947
This project has been put on hold.  Thanks for your help.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question