在UserControl

我有一个自定义WPF用户控件,其中使用了DatePicker。我正在使用at this SO article

提供的答案设置DatePicker的显示格式
<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>

我想为控件的不同实例使用不同的格式字符串,因此我想以某种方式在将UserControl添加到表单时提供格式,例如

<basecontrols:CustomControl 
    LabelWidth="{StaticResource LabelColumnWidth}" 
    Label="Custom Information" 
    DateDisplayFormat="dd-MMMM-yyyy" 
    />

Label和LabelWidth是自定义UserControl的Dependency属性。

当StringFormat位于Binding中时,是否可以将StringFormat绑定到控件属性?如果没有,有没有办法做我想做的事?

有意义的希望

nnn289 回答:在UserControl

  

StringFormat位于Binding内时,是否可以将StringFormat绑定到控件属性?

不。您不能绑定Binding的{​​{1}}属性,因为它不是依赖项属性。

您可以做的是在DateDisplayFormat中定义一个CustomControl依赖项属性(我想您已经完成了此操作),然后覆盖OnApplyTemplate方法并创建TextBox以编程方式。

或者,您可以在XAML标记中使用绑定到<MultiBinding>SelectedDate的{​​{1}},并使用返回DateDisplayFormat的多转换器。

本文链接:https://www.f2er.com/3165640.html

大家都在问