我目前使用Blend和一些我在网上找到的教程尝试使我自己的按钮用户控件。通常情况下,我只是使用自定义样式并将其应用于我想要的控件,但我也在某些依赖项属性中编写了代码,因此我决定编写自己的用户控件。设置内容时自定义按钮用户控件样式被覆盖
我无法理解的东西是如何设置Content属性而不覆盖按钮控件的样式。我认为这可能是我的代码,但是我用另一个按钮开始了全新的操作,并且发生了同样的事情 - 当设置Content属性时,按钮只是变成白色,并且左上角有黑色文本。
这里是掺和我生成的XAML代码:
<UserControl x:Class="MyUI.Controls.MyButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="25" d:DesignWidth="100">
<UserControl.Resources>
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Rectangle/>
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True"/>
<Trigger Property="IsDefaulted" Value="True"/>
<Trigger Property="IsMouseOver" Value="True"/>
<Trigger Property="IsPressed" Value="True"/>
<Trigger Property="IsEnabled" Value="False"/>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid>
<Button Content="Button" Height="25" Style="{DynamicResource MyButtonStyle}" Width="100"/>
</Grid>
现在,如果我在这样的主窗口引用按钮,它会恢复这是用户中完成的任何样式控制:
<local:MyButton Content="test" />
为了使按钮接受不同的内容标签,我需要更改什么?
您是否在UserControl中创建了内容依赖项属性?如果不是,则将“测试”分配给UserControl的“内容”属性,该属性的原始内容为“网格”。 –
我想我不太清楚你通过创建依赖属性的意思......我继续在增加:DependencyProperty的ContentProperty但是,这并不会对按钮文字的任何影响(这保持了风格,但现在犯规秀文字在所有) – jyanks