Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Silverlight Custom Control Binding

Posted on 2011-09-15
10
Medium Priority
?
910 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 668 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 668 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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 664 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

721 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