Setting a converter on a GroupDescriptor in Silverlight 4 - silverlight

I have a read-only datagrid bound to a domain data source. The data that I'm binding to has an ID property that I'm resolving on the grid through a converter (a simple int -> string mapping). The domain data source also has a GroupDescriptor on it, and this works, except I'm grouping by that column which has a converter on it.
Unfortunately the group header doesn't use the Converter and therefore just displays the ID, which is not desirable. I can replace the control template for the group header and explicitly use a converter on the template, but this is obviously not an ideal solution as I'm hard-coding the template to the converter.
Is there a way to use a converter on a group descriptor?

i found out that if you create a PropertyGroupDescription in code, it allows you to supply an IValueConverter in the constructor, so this would appear to be an answer.

Related

Make ValidationRule also apply to other list object

Suppose I have a object ListCollectionView and bind it to a datagrid. I bind the object's Name property to a TextBox(one of the datagrid's column)'s Text property and add my specific ValidationRule.
Now I want to the rule to check if the Name property is duplicated, I need to check other objects in the ListCollectionView, if there are objects' Name property equals to this Name, the validate result will be false.
My question is this check can apply only to the edited object, So only this object will become red when its Name is duplicated, but how can I also make check to other list objects, make the other objects which Name property equals to this edited one also become red? Thanks.
You are correct that the ValidationRule only has access to the data it is bound to. However you can write a custom ValidationRule - I've got a blog post detailing it here, and you can then add extra properties to it so you can inject the appropriate data that your bound value should be checked against.
However you will strike one tricky problem - the cells in the datagrid have no natural access to the DataContext of the datagrid because datagrid columns are not in the visual tree. To get around that problem you can use a static proxy object which contains (binds to) the ListCollectionView, this proxy object can then be bound to by the ValidationRule (example 1, example 2).

Passing QueryString Value For Silverlight Grid Row

How is it possible to do multivalue binding in Silverlight?
I have to determine the Visibility of a Silverlight DataGrid column depending on the value present as part of Datacontext and other one from the QueryString.
I use MVVM Model of silverlight 5 and my plan is currently to define a property for querystring in code behind that can be binded to row visibility. But my problem clearly here is to pass multiple values for the IValueConverter implementation.
Can anyone provide a simple example to solve my problem?
Multi binding is not supported out of the box in Silverlight.
But with the introduction of customer markup extensions in Silverlight 5, this can be achieved.
There's a good example on code project: http://www.codeproject.com/Articles/286171/MultiBinding-in-Silverlight-5.
Alternatively, in this particular example you can have public boolean a property in your view model which uses the QueryString Value along with the other value you are concerned in DataContext and decides whether the column needs to be visible or not. You can then databind this property to your column's IsVisible property. (Along with a value converter which returns Visbility.Visble /Visibility.Collapsed depending on the value of the boolean property value)

How do I sublass a control along with its attached properties?

I am trying to subclass System.Windows.Controls.DataGrid in order to add special commands to which a view model can bind. How do I do this and still have the Columns attached property? Do I have to declare it against my new type in order to use it?
Columns is not an attached property.. you should use it the same way you always do, just replace DataGrid with your-control-name:
<my:MyDataGrid>
<my:MyDataGrid.Columns>
<!-- Columns goes here --->
</my:MyDataGrid.Columns>
</my:MyDataGrid>

WPF and MVVM: Changing data binding converter at runtime

I am using WPF and the MVVM pattern in my user interface. In my ViewModel I have a List containing distances in millimetres, which I display in a ListView by binding ListView.ItemsSource to the List. However, I would like the values displayed to use a more natural unit - either metres or feet depending on the state of a "metric" checkbox.
I have written a couple of simple classes, MillimetresToMetresConverter and MillimetresToFeetConverter, both of which implement IValueConverter. Although I can set the Converter property on my data binding to one or the other, I am unsure how to change between these converters when the state of the checkbox changes.
My plan was to have a field "IValueConverter lengthConverter" on my ViewModel which I could set to one converter or the other, then in my XAML do ...="{Binding Converter={Binding Path=lengthConverter}}" - unfortunately this does not work since Converter is not a dependency property.
How can change the converter used by the data binding at runtime?
Most of the time when using the MVVM methodology, you can do the formatting task in the VM classes. In your case, you could add a Format property to the VM class, and based on the value of the Format property, return a well formated string.
See this discussion for more information.
If I may suggest a simple alternative solution: Create a small FormatMillimetresConverter in your ViewModel, whose UseMetric property is bound to the "metric" checkbox.

Are value converters instantiated per-binding in WPF?

Is a separate WPF value converter object instantiated for each binding that a particular value converter class is used in?
I am trying to create a two-way bit-to-boolean value converter. I would like to be able to bind a bool property (such as IsChecked) to a bit in a value type (like a ushort). I'm using the converter's parameter arguments to specify the bit. Implementing the ConvertBack() method is easy, but Convert() is little trickier.
In Convert() I need to know what the value of the entire ushort is so I can toggle just the single bit I am interested in. I was thinking of just using a member variable in my value converter class to temporarily store this whenever ConvertBack() is called, thus leading to the above question: does each binding get its own value converter instance?
If you use a converter defined in your resources, it will be shared amongst your properties.
If you need unique converters:
If you create a specific converter for a property, however, it will not be shared. Either option is workable. You can even use multiple converters (of the same type, with different keys) in your resources, which will create unique instances.
Create a constructor and destructor in your converter and set breakpoints within to tell for sure. I just created a simple example and it looks like only one converter was created for my multiple viewmodels that were using the constructor

Resources