I want a textbox which will always applied a Converter on the binding. this way I don’t need to specified the converter, the style will already contains the converter to use.
This is what I already tried:
I have a ViewModel bound to view. I override the datacontext of a textbox within this view using a property of the view model. It allows me to use a generic style for my text box
I have the following Style:
<Style x:Key="DateTimeTextBox" TargetType="TextBox">
<Setter Property="ToolTipService.ShowDuration" Value ="40000"/>
<Setter Property="Text" Value ="{Binding Path=.,Converter={StaticResource myConverter},UpdateSourceTrigger=PropertyChanged}"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="Unknown date format"/>
</Trigger>
</Style.Triggers>
</Style>
In the view I want to have the following text box bound on the porperty DateTimeValueToBind instead of the whole view model:
<TextBox Style="{StaticResource DateTimeTextBox}" DataContext="{Binding DateTimeValueToBind}"/>
The property DateTimeValueToBind is displayed properly in the textbox however it’s not bound anymore (when I modified it, the viewmodel property is not changed)
I tried to add the Mode=TwoWay in the binding options but It did not change anything.
Could explain why the DateTimeValueToBind property is not bound anymore in this context?
Thanks in advance
Leave the
DataContext, bind theTexttoDateTimeValueToBind(move the binding from theDataContexttoText). You should never set such a binding in a style; if the path is.you cannot bindTwoWay. (And what did you save anyway? You even have more to write as"DataContext"is longer than"Text".)