【问题标题】:how to set Lookless Custom Control Background?如何设置 Lookless 自定义控件背景?
【发布时间】:2013-05-01 15:03:51
【问题描述】:

我有一个自定义外观较少的控件。我创建了一个 Style 并在 style 中设置了依赖属性。

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

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

  // Few dependency properties here...

}

然后我在主题文件夹中的 Generic.xaml 中定义了布局,并指定了上述控件的目标类型。

数据绑定到控件的一切都很好。

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

当我像这样在 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>

感谢您的宝贵时间!

【问题讨论】:

    标签: wpf


    【解决方案1】:

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

    Background={TemplateBinding Background}
    

    有关更多信息,请查看此link,在“使用模板绑定保留控件属性的功能”部分下。

    【讨论】:

      猜你喜欢
      • 2011-01-01
      • 2011-08-22
      • 2018-08-10
      • 2018-06-29
      • 2021-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      相关资源
      最近更新 更多