Solved

Enable/Disable Button Using Custom Binding Logic in  WPF

Posted on 2012-03-22
10
1,265 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SqlDataBase 7 48
Adding Existing Item to vb.net Project causes Errors 4 32
SqlServer no dupes 25 34
C# Gridview 1 32
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 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