SilverLight - Binding a command to button within control template - silverlight

I have a datagrid. Within that datagrid, I have bunch of column headers styles. Inside the control template of this style, there is a button. I need to bind a command to that button.
Note that, there is also a TextBlock within the header style which I bind using element to element binding, as I will not have my viewModel in the header's dataContext.
If I use the same idea to bind the button command, it doesnt work. Am I missing something?
If this is unclear, please let me know and I will post sample code.
Thanks in advance!

Sample code always helps :-)
Remember that each data row in your grid is bound to an item with your datagrid datacontext. If your command does not exist in your item class (which it probably shouldn't) then it won't get bound correctly.
Can you trigger your command from the SelectedItem changed property of your datagrid, rather than an internal button? You can do this with the following code: http://thoughtjelly.blogspot.com/2009/12/silverlight-commands-data-grid-row.html

Related

ListBox Item style

I need to write a style for a listboxItem that will add a button on the right of the listboxitem when it is selected or mouse over. The button need to have a command binding as well .
This style need to be used in different places.
also the button won't have a border. but when the mouse is over the button , it will have a border.
How can i write this?
I tried to write a style, but i can't bind to the command of the button that is inside the style.
Code used: (sorry i wasn't able to paste my code normally here so it is here http://justpaste.it/1b9s)
It would be great to see some code but an option that should work is to create a DataTemplate. You can set it on ListBox ItemTemplate property and use it in the different locations where you may need it.

Silverlight: Individual visibility of a RowDetailsTemplate for each data row

I'm trying to achive displaying the RowDetailsTemplate of a Silverlight DataGrid depending on a bool Property, bound to a CheckBox Control's IsChecked Property. Insinde of my RowDetailsTemplate there is a single custom UserControl, containing further Controls.
Since the DataGrid only allows a global setting (RowDetailsVisibilityMode) Some code-behind is needed. I've implemented a solution based on Rorys Reply (and using a behaviour-technique) which actually works.
Unfortunately, The DataGrid doesn't remember the individually shown or hidden Rows on sorting. The checkbox remains selected, but the Row collapses. Further, no event like "OnAfterSort" or something similar seems to exist, where i could "Refresh" the visibility settings in a loop.
Another idea was to bind the Visibility of my custom Details-UserControl to the CheckBox bound value. This actually works (when settings RowDetailsVisibilityMode to "Visible"), but I'm not able to get rid of this weird behaviour: When the CheckBox is checked, the Detail Template expands and the detail UserControl appears. Nice. When the CheckBox is unchecked again, the UserControl disappears (Visibility is set to Collapsed) but the Row doesn't collapse and the blank space remains (as it would be set to Hidden not Collapsed).
Do you have any ideas?
I hope it's ok I didn't post any code samples, the implementation is pretty easy and I believe that the problem doesn't actually lie in a coding mistake i made. You can setup a simple DataGrid quickly like in this perfect MSDN Example. Starting from here, it's easy to test both described behaviours!
Really big thanks in advance,
- Thomas

Silverlight ItemsControl - change ItemTemplate

I have an ItemsControl with a list of radio buttons - in this case the ItemTemplate will contain a binded radio button.
The problem is that in some cases I need to replace the radio button with a check box without using any C# code. Is this possible? I thought at using a ValueConverter (C# code) but I don't know for sure if will work.
You could put both a radio button and a checkbox in the template, then use a binding expression in the "Visible" property that ensures only one of them is visible based on the source object being bound. If your logic to decide visibility is more complex than just a boolean on the source object, use a valueconverter in the binding expression.

How can I change textbox style in a textbox style data trigger?

I have created a default style for my textbox controls, but I would like to be able to change the style at runtime.
I have a property that I want to bind to that informs the UI if the data is mandatory or not, and I want to be able to change the style of the textbox to another style that displays a red line under the textbox. I can't use this in the standard textbox style I've created because the property is different for each textbox i.e. IsEmailMandatory, isNameMandatory.
I've tried to bind a data trigger in the style of the textbox for each textbox I have, but I get a message telling me I can't change a style within a style. so how do I do this?
You don't need to change the whole style in the trigger, just the template.
Another way to achieve something similar would be to use attached properties. Assuming that the IsEmailMandatory, isNameMandatory properties are on the DataContext not the control itself, you could then bind the value you want to an attached property and then trigger off that in your default style.

WPF listview databinding - How do I force update to underlying object without tabbing out?

I have a WPF ListView that is bound to a BindingList<T>. The binding works like a charm, but I have to tab out of the cell to get the bound property to update....this is an issue because most users don't tab out of the last column before clicking the save button.
How do I force the list view to "persist" the changes to the bound DataContext without doing something hackish.
Bindings in WPF have a property called "UpdateSourceTrigger" which tells the Binding when to update the thing the UI is bound to. By default, it's set to "LostFocus" for the Text property which is what you're most likely using.
Change the trigger to "PropertyChanged" in your binding like this:
Text="{Binding Foo,UpdateSourceTrigger=PropertyChanged}"
... and now the source "Foo" property will be updated as the Text changes in the UI.
There's also an "Explicit" setting for UpdateSourceTrigger which is handy if you need to hold off writing any changes to the source until, say, the user clicks the OK button.

Resources