Reputation: 542
I have a custom WPF UserControl that uses a DatePicker within it. I'm setting the display format of the DatePicker using the answer provided at this SO article
<Style TargetType="{x:Type DatePickerTextBox}" BasedOn="{StaticResource {x:Type DatePickerTextBox}}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<TextBox x:Name="PART_TextBox"
Text="{Binding Path=SelectedDate, StringFormat='dd-MM-yy', RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I would like to use a different format string for different instances of the control, so I'd like in some way to provide the format when I add the UserControl to the form, something like
<basecontrols:CustomControl
LabelWidth="{StaticResource LabelColumnWidth}"
Label="Custom Information"
DateDisplayFormat="dd-MMMM-yyyy"
/>
Label and LabelWidth are Dependancy properties of the custom UserControl.
Is it possible to have bind the StringFormat to a control property, when it is inside a Binding ? If not, is there a way to do what I want to do?
Hope that makes sense
Upvotes: 0
Views: 200
Reputation: 169210
Is it possible to have bind the
StringFormat
to a control property, when it is inside aBinding
?
No. You can't bind the StringFormat
property of a Binding
because it's not a dependency property.
What you could to is to define a DateDisplayFormat
dependency property in your CustomControl
(which I guess you have done already) and then override the OnApplyTemplate
method and create the binding of the TextBox
programmatically.
Alternatively, you could use a <MultiBinding>
in the XAML markup that binds to both SelectedDate
and DateDisplayFormat
and use a multi converter that returns a string
.
Upvotes: 1