Probably a dumb question but to make a default value for binding in a WPF ControlTemplate what is the best way in terms of performance?
I've tried several option:
- Priority binding: It didn't work, probably my bad...
ControlTemplate Trigger: Works well, I use it on a property which always return true in my case:
<ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="True"> <Setter Property="TextBoxWidth" Value="300" /> </Trigger> </ControlTemplate.Triggers>
FallBackValue: Works well too.
Is it better to work with trigger, FallBackValue or is there another way to do it?
Thanks in advance
----------------- UPDATE ----------------
After doing more research it seems that the best way to achieve my goal is simply using the styles... I don't know why it didn't work in the first place but just doing this seems to work:
<Style TargetType="local:EditableWidthText">
<Setter Property="TextBoxWidth" Value="300"/>
</Style>
No need for FallbackValue anymore. It works when there is a binding, when there is no binding and when it's overrided by another style. Exactly what I need.
Thanks Sinatr for your comment about performance of FallbackValue. Fyi Priority Binding:
PriorityBinding lets you associate a binding target (target) property with a list of bindings.
The first binding that returns a value successfully becomes the active binding.
https://msdn.microsoft.com/en-us/library/system.windows.data.prioritybinding%28v=vs.110%29.aspx
I never used template selector nor datatemplate, but I think it's a bit overkill for my need, no?
----------------- UPDATE #2----------------
@Sinatr: I've a Mainviewmodel which contains several ViewModel binded to several usercontrols displayed into the MainView. (Something like one of my other post: Navigate through UserControl with MVVM LIGHT (WPF))
But this controlTemplate is for a looklessControl (something quite simple, juste a label and a textbox).
In this LookLess Control I've created 4 dependency properties. In the control template I've binded 2 of them to the Text/Content propertie of the Textbox/Label and two to their width. (I want the user to be able to create forms in the future, binding way). For example the label looks like this in the ControlTemplate:
<Label
Width="{Binding Path=LabelWidth,
RelativeSource={RelativeSourceAncestorType=local:EditableWidthText}}"
Content="{Binding Path=LabelText,
RelativeSource={RelativeSource AncestorType=local:EditableWidthText}}"/>
I just want the width to be equals to 130 when there is no binding defined. But I don't want problem of performance because of a binding failed or something like this.
Template selector are very interesting, but I don't think that applied in my case. There is no switching within the datas, it's just a default value. So I don't think that apply either. (But maybe I'm wrong :p )
So maybe I think that styles are the way to go?