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
Solved

Silverlight Custom Control Binding

Posted on 2011-09-15
10
895 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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