首页 文章

如何设置Lookless Custom Control Background?

提问于
浏览
1

我有一个自定义的外观较少控制 . 我创建了一个Style并在样式中设置了依赖项属性 .

如何在模板中设置控件的背景 . 我可以在没有明确声明依赖属性的情况下执行此操作吗?

public class AddressCustomControl:Control
{
   static AddressCustomControl()
   {
       DefaultStyleKeyProperty.OverrideMetadata(typeof(AddressCustomControl), new ....)
   }

  // Few dependency properties here...

}

然后我在themes文件夹中的Generic.xaml中定义了布局,并指定了上述控件的targettype .

对于控件的数据绑定,一切都很好 .

我不希望在使用控件时更改此控件的颜色以及字体和前颜色等其他一些属性 .

当我在xaml中指定时,没有任何反应:

<local:AddressCustomControl Address={Binding BillAddress} Background="Silver" /> // Background does not change when I do this.

我在这里错过了什么?可能我必须以自己的风格做点什么?

这是我的风格:

<Style TargetType="{x:Type controls:AddressCustomControl}">
      <Setter Property="Template">... setter value and the control template here...          </Setter.Value> // May be I need to do something here that I am missing?
</Style>

谢谢你的时间!

1 回答

  • 1

    在Generic.xaml文件中,您应该在控件上设置类似的内容

    Background={TemplateBinding Background}
    

    有关其他信息,请查看link,在"preserving the functionality of a control's properites using templatebinding."部分下

相关问题