我有我想要的風格自定義控制: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