我有
<StackPanel Orientation="Horizontal">
<Label Content="{Binding WidgetSubTitle}" Style="{StaticResource WidgetSubTitleStyle}" />
<DatePicker SelectedDate="{Binding SelectedDate}">
<DatePicker.Resources>
<Style TargetType="DatePickerTextBox">
<Setter Property="Visibility" Value="Collapsed" />
<Setter Property="IsReadOnly" Value="True" />
</Style>
</DatePicker.Resources>
</DatePicker>
</StackPanel>
看起来像
我想摆脱“日历”图标,而是将日期选择器“附加”到我的Label
。单击标签后,我想打开选择器(该选择器与DateTime
视图模型中的属性具有双向绑定)。我也可以将其替换Label
为按钮,因此可以从其命令中受益,这不是问题。
我不知道该怎么做。我已经看到它把一个一个的解决方案Calendar
的Popup
,但这似乎复杂的事情,对我来说。
我可以想象的一种解决方案是-隐藏整个DatePicker
控件(可见性=折叠),然后单击按钮/标签时,仅打开该选择器的日历弹出部分的可见性。虽然不能完全弄清楚样式...
您可以通过更改其ControlTemplate完全改变WPF控件的外观。在这种情况下,您可以为DatePicker控件定义一个自定义模板,该控件基本上使它看起来像一个Button,然后为Button定义另一个模板,使它看起来像Label。像这样的东西:
<DatePicker>
<DatePicker.Template>
<ControlTemplate TargetType="DatePicker">
<Grid x:Name="PART_Root" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<Button x:Name="PART_Button">
<Button.Template>
<ControlTemplate TargetType="Button">
<Label Content="{Binding WidgetSubTitle}" Style="{StaticResource WidgetSubTitleStyle}" />
</ControlTemplate>
</Button.Template>
</Button>
<Popup x:Name="PART_Popup" AllowsTransparency="True" Placement="Bottom" PlacementTarget="{Binding ElementName=PART_TextBox}" StaysOpen="False"/>
</Grid>
</ControlTemplate>
</DatePicker.Template>
</DatePicker>
这样做的好处是,即使更改控件的外观,控件的功能也保持不变。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句