Solved

Enable/Disable Button Using Custom Binding Logic in  WPF

Posted on 2012-03-22
10
1,282 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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