Link to home
Start Free TrialLog in
Avatar of weimha
weimhaFlag for United States of America

asked on

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

SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of weimha

ASKER

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.
Avatar of somanadha
somanadha

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?
Avatar of weimha

ASKER

somandha, I don't know what the "fallbackvalue" is.  Tthe user's want to see the busy indicator.
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
Avatar of weimha

ASKER

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}" >


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of weimha

ASKER

This project has been put on hold.  Thanks for your help.