Silverlight - Sample Data - Complex objects - silverlight

I really like using the sample data binding at design time for Silverlight.
It is easy to use it when you need to return string values.
In my case, I am trying to bind to my ViewModel one of whose properties returns a SolidColorBrush. How do I setup the sample data to return a SolidColorBrush value?
Here is what the string based properties look in the sampleData.xml file.
<ViewModels:MyViewModel AlphaValue="Abcd" ColorValue="????"/>
How should I format ColorValue to return a SolidColorBrush?
What I am trying to do:
I have a textblock with its foreground set to Foreground="{Binding ColorValue}". The TextBlock is invisible on the design surface as its not getting a value from the sampledata.xml file. Where as another textblock on which I have only text property set to a binding value, appears correctly on the design surface.

You need to look at creating a value converter. Look at this and it should give you an understanding of what you need to do. http://msdn.microsoft.com/en-us/library/system.windows.data.binding.converter.aspx

Related

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)

Setting a converter on a GroupDescriptor in Silverlight 4

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.

Silverlight - add ValueConverter to Binding object in custom control

I'm building a custom control in Silverlight, extending TextBox. The purpose of the control is to provide a watermark logic (default text, typically used in search boxes). I managed that, when accessing the Text property, it will return string.Empty if Text == Watermark. Indeed, you don't want to consider something like "Enter name here" as a relevant value. When it comes to TwoWay databinding, things get more complicated.
I created a ValueConverter, that takes as parameter the watermark and returns string.Empty if Text == Watermark, Text otherwise. I want the control to be very ease to use, so it would be cool if the client code wouldn't have to specify each time that converter when binding to the Text property. Instead, the converter would be plugged inside the custom control, on the binding object related to the Text property.
I tried the following code, but it crashes because the binding object cannot be modified once it has been assigned. I tried that code in the Load() and OnApplyTemplate() events.
var watermarkedTextBox = (WatermarkedTextBox)dependencyObject;
var textBindingExpression = watermarkedTextBox.GetBindingExpression(TextProperty);
if (textBindingExpression != null)
{
var textBinding = textBindingExpression.ParentBinding;
textBinding.Converter = new WatermarkConverter();
textBinding.ConverterParameter = watermarkedTextBox.Watermark;
watermarkedTextBox.SetBinding(TextProperty, textBinding);
}
So I need to intercept the binding object at the right time (where it's still allowed to modify it). Any ideas ?
Thanks in advance,
Thibaut
All right, discussed this with colleagues, found the optimal solution.
The watermark is defined in the ControlTemplate of the custom control. It's a TextBlock added in the TextBox, hidden on focus, shown if text is empty. Code is much better like that :
No need to play with the Text property and change it under certain conditions to change it to watermark, or change it to string.Empty so the watermark text is never returned (was error prone)
Watermark text style can be directly template bound (TemplateBinding), so it's great, without any C# code, client will be able to customize the appearance of the watermark : color, italicize and more
Offers new possibilities (image watermark textbox almost for free)
See you ;)
I haven't tried it yet but the Silverlight 4 Textbox has a Watermark property.

Bind a SolidColorBrush to a DataGridCell/CellStyle from a ViewModel

I have a ViewModel with Property int DepartmentColor. Of course I can not bind the int value to a CellStyle in XAML.
Should I make a IntToStyleConverter or should I fumble around with the Style class in the ViewModel like convert the int to a SolicColorBrush and assign it to a Style etc...
Is the latter the way to go with MVVM ?
Go the converter route. In addition I would title the property something to the effect of Department and not tie color to it as it makes it more tightly coupled to the UI. While the department may be Accounting coupling that to a color implies you are certain that it will be represented via a Color, whereas down stream it may be represented in some other visual fashion. You could also create the styles ahead of time and then simply select one and apply it via the converter, versus trying to create them in the code behind.

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.

Resources