?
Solved

Enable/Disable Button Using Custom Binding Logic in  WPF

Posted on 2012-03-22
10
Medium Priority
?
1,406 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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 1500 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 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