【发布时间】:2020-09-23 12:40:36
【问题描述】:
我有一个基于按钮的自定义控件,可以包含如下文本:
public class TextButtonControl : Button
{
public bool AllCaps
{
get => (bool)GetValue(AllCapsProperty);
set => SetValue(AllCapsProperty, value);
}
public static readonly DependencyProperty AllCapsProperty =
DependencyProperty.Register("AllCaps", typeof(bool), typeof(TextButtonControl), new PropertyMetadata(false));
public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(TextButtonControl), new PropertyMetadata(""));
static TextButtonControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TextButtonControl), new FrameworkPropertyMetadata(typeof(TextButtonControl)));
}
}
<ControlTemplate TargetType="{x:Type local:TextButtonControl}">
<Border Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" BorderBrush="Red" BorderThickness="2">
<TextBlock Text="{TemplateBinding Text}"
FontFamily="{TemplateBinding FontFamily}"
FontSize="{TemplateBinding FontSize}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
TextAlignment="Center"/>
</Border>
</ControlTemplate>
当依赖属性 AllCaps 更新时,如何使控件的文本从大写变为小写(反之亦然)?
示例用法:
<!-- inside some window or user control -->
<ToggleButton x:Name="tbCaseToggle" />
<custom:TextButtonControl Text="some text" Capitalised="{Binding ElementName=tbCaseToggle, Path=IsChecked}" />
在切换按钮被选中或取消选中时更改按钮内容的大小写。
【问题讨论】:
标签: c# wpf .net-core custom-controls