Wpf 设置内容时覆盖自定义按钮用户控件样式

Wpf 设置内容时覆盖自定义按钮用户控件样式,wpf,xaml,button,user-controls,Wpf,Xaml,Button,User Controls,我目前正在使用Blend和一些在线教程来尝试制作自己的按钮用户控件。通常,我只会使用自定义样式并将其应用于我想要的控件,但我也编写了一些依赖属性,因此我决定编写自己的用户控件 我无法理解的是如何设置Content属性而不覆盖button控件的样式。我以为这可能是我的代码,但我用另一个按钮开始了全新的工作,我也有同样的事情发生——当设置Content属性时,按钮只会变成白色,左上角有黑色文本 下面是Blend为我生成的XAML代码: <UserControl x:Class="MyUI.Co

我目前正在使用Blend和一些在线教程来尝试制作自己的按钮用户控件。通常,我只会使用自定义样式并将其应用于我想要的控件,但我也编写了一些依赖属性,因此我决定编写自己的用户控件

我无法理解的是如何设置Content属性而不覆盖button控件的样式。我以为这可能是我的代码,但我用另一个按钮开始了全新的工作,我也有同样的事情发生——当设置Content属性时,按钮只会变成白色,左上角有黑色文本

下面是Blend为我生成的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的Content属性是其唯一的子元素,在您的示例中是Grid。您可以创建自己的内容属性,也可以创建另一个具有不同名称的内容属性。在UserControl的.cs文件中:

/// <summary>
/// Interaction logic for MyButton.xaml
/// </summary>
public partial class MyButton : UserControl
{
    public new static DependencyProperty ContentProperty =
        DependencyProperty.Register("Content", typeof (object),
                                    typeof (MyButton));

    public new object Content
    {
        get { return GetValue(ContentProperty); }
        set { SetValue(ContentProperty, value); }
    }

    public MyButton()
    {
        InitializeComponent();
    }
}
//
///MyButton.xaml的交互逻辑
/// 
公共部分类MyButton:UserControl
{
public新静态DependencyProperty ContentProperty=
DependencyProperty.Register(“内容”)、类型(对象),
类型(MyButton));
公共新对象内容
{
获取{返回GetValue(ContentProperty);}
set{SetValue(ContentProperty,value);}
}
公共MyButton()
{
初始化组件();
}
}
在取消控制的Xaml上:

<Button Content="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 
            Height="25" Style="{DynamicResource MyButtonStyle}" Width="100"/>


现在按钮的内容绑定到UserControl级别的内容属性。

您是否在UserControl中创建了内容依赖项属性?如果不是的话,您正在将“test”分配给UserControl的内容属性,它的原始属性是Grid。。。我继续添加了:DependencyProperty ContentProperty,但这对按钮文本没有任何影响(它现在保持样式,但根本不显示文本)嘿-这是完美的减去一件事。。。现在,在我的designer视图中,我得到一个错误:“指定的元素已经是另一个元素的逻辑子元素。请先断开它。”。我知道这与设置内容属性有关,但我不知道如何解决这个问题。有什么想法吗?将XAML{x:Type UserControl}上的代码修改为{x:Type local:MyButton},因为您破坏了继承,所以需要让设计器知道您正在使用特定UserControl中的内容。