2013-07-08 38 views
1

我目前使用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" /> 

为了使按钮接受不同的内容标签,我需要更改什么?

+0

您是否在UserControl中创建了内容依赖项属性?如果不是,则将“测试”分配给UserControl的“内容”属性,该属性的原始内容为“网格”。 –

+0

我想我不太清楚你通过创建依赖属性的意思......我继续在增加:DependencyProperty的ContentProperty但是,这并不会对按钮文字的任何影响(这保持了风格,但现在犯规秀文字在所有) – jyanks

回答

2

您需要将UserControl级别的Content属性连接到Button级别的Content属性。默认情况下UserControl的Content属性是它唯一的子元素,在你的情况下它是Grid。您可以创建自己的内容属性或另一个具有不同名称的内容属性。在你的用户控件的.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(); 
    } 
} 

在您UnserControl的XAML中:

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

Now按钮的内容结合到你的用户控件级别的内容属性。

+0

嘿 - 这是完美的减一件事......现在,在我的设计师看来,我得到的错误:“指定的元素已经是另一个元素的逻辑子,先断开它。”。我知道它与设置Content属性有关,但我不知道如何解决该问题。有任何想法吗? – jyanks

+0

将XAML {x:Type UserControl}上的代码修改为{x:Type local:MyButton},因为您破坏了继承,所以您需要让设计者知道您正在使用特定UserControl中的内容。 –

+0

你是救命恩人,谢谢! – jyanks