Solved

Enable/Disable Button Using Custom Binding Logic in  WPF

Posted on 2012-03-22
10
1,307 Views
Last Modified: 2012-03-22
I have a number of buttons that perform actions on the selected record in a bound datagrid.

I only want to enable the buttons if a record is selected and the values of certain properties for the selected item meet the right condition.

I'd like to do this by binding to the isEnabled property of the button but I can't see how I can do this carrying out the tests above.

Could someone please provide an example of how this could be achieved

Thanks
0
Comment
Question by:monserob
[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
  • 5
  • 5
10 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 37752400
1) Bind your button to an ICommand and in the can execute do the validations

2) Bind is enabled to a property and in the getter of the property do the validation.

in both cases when the selected item changes along with firing property changed for the selected item in the setter ,call property changed for the property and if it's command, invalidate can execute
0
 

Author Comment

by:monserob
ID: 37752586
I considered option 2 but it won't handle the situation where the selected item is nothing i.e. the bound collection is empty.

I've not used commands before so can you confirm whether it can handle this scenario and perhaps provide an example
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 37752597
i didnt understand what u meant by it didnt handle selected item is null.. can you post the code that u tried ?
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

Author Comment

by:monserob
ID: 37752693
Difficult to post the code but basically in the code-behind for my window I set the ItemsSource property of my datagrid (grdVists) to my collection object (visits).

In the Xaml at the window level I have this line of code:

DataContext="{Binding ElementName=grdVisits, Path=SelectedItem}"

Currently for the buttons I have logic like this:

IsEnabled="{Binding Path=IsPreInstallCheckRequired}"

IsPreInstallCheckRequired is a property on the visit object inside the visits collection so if there are no visits then this property can't be evaluated. I would like the button to be disabled in this scenario.
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 37752796
why do u need to set the data context to selected item.

normally , there will be a view model which will have IsPreInstallCheckRequired

and the getter will be something like this

if(SelectedItem != null)
{
// do ur stuff
}

and the selected item will be bound to selecteditem in model

SelectedItem="{Binding SelectedItem}"
0
 

Author Comment

by:monserob
ID: 37752914
I'm not using MVVM and have no desire to. The rational behind setting the data context to the selected item came from  Pro WPF in C# 2010.
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 37752947
have u used converter ? you can use a converter to do the logic there and change binding to

IsEnabled="{Binding ElementName=grdVisits, Path=SelectedItem,Converter={StaticResource conv}}"
0
 

Author Comment

by:monserob
ID: 37753019
Yes, I was attempting to use a converter this morning but wasn't getting very far. I've implementing your suggestion and it's fixed the issue with the button being enabled when an item isn't selected. The code in my Convert function is:

 If Not (value Is Nothing) Then
            Return True
        Else
            Return False
        End If

Now I need to handle the second part of my problem i.e. enable the button(s) if the required properties for the selected object meet certain conditions. How can I do this extending the converter suggestion?
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 37753079
u can cast value to your data item in the converter and then do the logic in the else part.

If Not (value Is Nothing) Then
            Return True
        Else
{
// do stuff if not null
            Return False
}
        End If
0
 

Author Comment

by:monserob
ID: 37753243
That works a treat thanks!
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.

696 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