How can I make a XAML style override a binding? - silverlight

This question is for WinRT, but may also be applicable for Silverlight. Say I have databound the Background property of a ListView/ListBox, but I want a that databinding only to be in place when a particular theme is applied. I've implemented themes using Merged Dictionaries of XAML styles. When a different theme is applied, I want it to be statically defined by the style.
Is there a way to achieve this using XAML only?
I've tried placing the "Style" attribute after "Background" in the ListView tag itself, to see if the order of the properties mattered, but that did not seem to have any effect.

Nilzor -
This seems like the kind of place where you would use a Custom Converter. In this way, when the binding happens you can run logical tests and any arbitrary code to return the a value that is acceptable for binding.
WinRT project come with an example of the custom converter which i believe is named BooleanToVisibility Converter.
For the record: This does not NEED to be a conversion (i.e. bound object is bool, convert to Visibility and return it to the Visibility property) it can be a logical test -- The bound object is XYZ derives from ABC & if XYZ.Parent.SomeProperty == someValue return different ABC.
Here is a stack overflow link for Creating / Implementing them:
Binding to a property of a custom converter

Related

How to bind ItemsControl's ItemsSource property by using naming conventions in caliburn.micro correctly?

There is an ItemsControl named Items in the view and a ViewModel contains a BindableCollection<T> property Items property, I want them get binding correctly by using the naming conventions mechanism in caliburn.micro, however it dose not works as expected.
When I add ItemsSource="{Binding Items}" in the view explicitly, it works, I am wondering what's the key point missed when using naming conventions in the ItemsControl?
Firstly, using the word "Items" as a name for a property, control etc is a bad choice. It's confusing. This is true if someone is maintaining your code or reading a question. You should have considered names for things.
The conventions for caliburn micro are explained here:
https://caliburnmicro.com/documentation/conventions
Essentially, a control is examined.
The name is used to look up a property from the datacontext.
If they match then a dependency property of that control will be bound.
Which dependency property is used varies from control to control. Text is the default for TextBox and TextBlock. ItemsSource is the default for an itemscontrol.
Hence the answer to your question is your control name should match the property name.
As they seem to, this is not why your binding is failing.
You also need to provide an instance of that datacontext somehow.
You proved that is happening with your working explicit binding.
The two most likely possible causes are therefore:
Your bootstrapper isn't running the caliburn micro wireup code.
or
Your view isn't provided by the bootstrapper and you've not used viewmodel.bind on it to wire everything up.
or
You've somehow interfered with the default binding conventions.
PS
https://caliburnmicro.com/announcements/stepping-away

Generic ViewModel that works with ItemsControl

I implemented some generic CustomControls in WPF, for instance an AutoCompleteTextBox.
Now, I'd like to implement a generic ViewModel library, in order to perform the databind of these controls.
Now I defined one attached property named CDataSource, that specifies the source of the data to bind within the control.
My question is : Is it possible, that the CustomControl passes to the ViewModel the CDataSource value? In this way the ViewModel may populate the control on the basis of the CDataSource property.
Thanks in advance
This seems like a strange request to me. You don't want any dependency on your view model from within your custom control. Instead, you would normally have a dependency property on your custom control which is the ItemsSource, and then you would set the value of this from your view in XAML.
This is how the AutoCompleteBox included in the WPF Toolkit operates.

WPF binding to a non dependency property on a control template

I'm creating a WPF custom control as an auto learning exercise. My control has a ListView inside the template. I wanto my control user be able on defining the needed columns in his own Xaml, but I did not get the strategy on how to pass the columns to the inner listview since binding with FindAncestor complain that "Columns" is not a DependencyProperty.
Wekk the questions are:
How to achieve bind a property from xaml to the template when it is not a DP
Correct my design: I think there is something wrong: if someone would change completely my template, how should I let him use the Column collection ?
why not inherit from ListView directly? Then you have all the properties you need for the ListView and can also add you own properties to the class.
Then you can apply a custom Style to your control to make it look like you want. (Here you have a basic ListView Style that you can use and expand to your needs)
Sometimes binding to a property that is not a dependency property can be solved using the Binding Mode OneWayToSource
Have you tried that?

WPF : derived usercontrol from a control in a different project. How to make it use the same style?

I'm using the 'Extended WPF Toolkit' ( http://wpftoolkit.codeplex.com/ ),
and for my own purposes I've created a generic version of its NumericUpDown control called GNumericUpDown< T > which actually lets me specify what type to use, ie. GNumericUpDown< int >.
(This is done to make sure the control respects the appropriate Min/MaxValues of the wanted type)
To be able to use different types from Xaml, I've created a new project with specific derived versions, f.e. NumericUpDownFloat which is derived from GNumericUpDown< float>.
But when I use the NumericUpDownFloat in XAML, nothing is displayed.
I assume this is because there's only a style specified for the WPF Toolkit's NumericUpDown in the Generic.xaml resourcedictionary of the WPF Toolkit project.
So how can I make all my specific versions (NumericUpDownInt, NumericUpDownByte, ..) actually use that style ?
You should get the latest source code for the Extended WPF Toolkit. The updated NumericUpDown control allows you to specify what data type to use in the editor. The following code specifies to use an Int32 as the data type instead of the default double. As you can see this is done by setting the ValueType property on the NumericUpDown control.
<extToolkit:NumericUpDown Grid.Row="1" Value="{Binding Age}" Increment="1" Minimum="18" Maximum="65" ValueType="{x:Type sys:Int32}" />
This will eliminate the need for the seperate project with specific dervied controls.
By default when you make any type of custom control, WPF puts this in the static contructor of the class for you:
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
This tells WPF that you are going to supply a theme for it somehow. By default, a Themes folder will be created at the root of the project, with a Generic.xaml file created with the CustomControl1 style and control template put inside of it.
If you want to tell WPF you will be override the style (or really, overriding the default style key dependency property), you just put that line in you static constructor. If you want it to fall back to whatever it's parent style is, you just omit that line and something like this:
public class SuperAwesomeControl : Border
{
....
}
Will always look like border by default.

Changing DataTemplates at Runtime

I'd like to be able to swap out data templates at runtime, but without the
FindResource("fdds") as DataTemplate
Type of code I've seen a lot. I'd like to be able to just bind the template to look for the resource according to a property in my ViewModel. Conceptually I'd like to be able to do this, but the compiler obviously doesn't like it:
... ItemTemplate="{StaticResource {Binding Path=VMTemplate}}">
And then other commands would change the value of VMTemplate in the ViewModel. Is there a way to do something like this?
StaticResource extension is an immediate lookup when the XAML is parsed which means the Resource must be present at the start of the app. In order to set a template dynamically you will have to do something similar to the way your first line looks.
A possibility I have seen would be to make the DataTemplate have a custom control that extends ContentControl that has multiple DataTemplate properties that would then be able to select different templates based on a bound value from your View Model.

Resources