Solved

Enable/Disable Button Using Custom Binding Logic in  WPF

Posted on 2012-03-22
10
1,233 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
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now