我有我想要的风格自定义控制:WPF的造型自定义控件
这仅仅是从文本框和其他接口继承的类,接口只增加了一个额外的属性。
如何将样式应用于此自定义控件,以便设置只读属性时,背景变为灰色?
public class DionysusTextBox : TextBox, IDionysusControl
{
public DionysusTextBox()
{
SetStyle();
}
#region IDionysusControl Members
public bool KeepReadOnlyState
{
get { return (bool)GetValue(KeepReadOnlyStateProperty); }
set { SetValue(KeepReadOnlyStateProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty KeepReadOnlyStateProperty =
DependencyProperty.Register("KeepReadOnlyState", typeof(bool), typeof(DionysusTextBox), new UIPropertyMetadata(true));
#endregion
#region Style
Style styleListBoxItem = new Style(typeof(DionysusTextBox));
Trigger triggerReadonly = new Trigger { Property = DionysusTextBox.IsReadOnlyProperty, Value = true };
private void SetStyle()
{
triggerReadonly.Setters.Add(new Setter(DionysusTextBox.BackgroundProperty, Brushes.Black));
this.Triggers.Add(triggerReadonly);
}
#endregion
}
以上是整个类的代码,我使用的样式似乎是适当的方式,但该方式,当我将此控件添加到设计师,我得到以下错误:
Triggers collection members must be of type EventTrigger.
任何人都可以指向正确的方向吗?
一个'Trigger'只能是应用于'Style'。在你的情况'styleListBoxItem'不是'this'。 – LPL
这么简单,我改变它,不再收到错误,但风格不起作用,有什么想法? –
我没有看到你应用了这种风格。 – LPL