C# WPF样式自定义控件
我有一个要设置样式的自定义控件: 它只是一个从TextBox和另一个接口继承的类,该接口只添加了一个额外的属性 如何将样式应用于此自定义控件,以便在设置只读属性时背景变为灰色C# WPF样式自定义控件,c#,wpf,C#,Wpf,我有一个要设置样式的自定义控件: 它只是一个从TextBox和另一个接口继承的类,该接口只添加了一个额外的属性 如何将样式应用于此自定义控件,以便在设置只读属性时背景变为灰色 上面是整个类的代码,我使用样式的方式似乎是合适的方式,但当我将此控件添加到设计器中时,出现以下错误: Triggers collection members must be of type EventTrigger. 有人能给我指出正确的方向吗?您可以重新定义依赖项属性的默认行为,特别是,您可以定义PropertyC
上面是整个类的代码,我使用样式的方式似乎是合适的方式,但当我将此控件添加到设计器中时,出现以下错误:
Triggers collection members must be of type EventTrigger.
有人能给我指出正确的方向吗?您可以重新定义依赖项属性的默认行为,特别是,您可以定义
PropertyChangedCallback
s:
public class DionysusTextBox : TextBox, IDionysusControl
{
static DionysusTextBox()
{
//For the IsReadOnly dependency property
IsReadOnlyProperty.OverrideMetadata(
//On the type DionysusTextBox
typeof(DionysusTextBox),
//Redefine default behavior
new FrameworkPropertyMetadata(
//Default value, can also omit this parameter
null,
//When IsReadOnly changed, this is executed
new PropertyChangedCallback(
(dpo, dpce) =>
{
//dpo hold the DionysusTextBox instance on which IsReachOnly changed
//dpce.NewValue hold the new value of IsReadOnly
//Run logic to set the background here, you are on the UI thread.
//Example of setting the BorderBrush from ARGB values:
var dioBox = dpo as DionysusTextBox;
//Should always be true, of course, it's just my OCD ;)
if (dioBox != null)
{
dioBox.BorderBrush =
ColorConverter.ConvertFromString("#FFDDDDDD") as Color?;
}
})));
//For the BorderBrush property
BorderBrushProperty.OverrideMetadata(
//On the type DionysusTextBox
typeof(DionysusTextBox),
//Redefine default behavior
new FrameworkPropertyMetadata(
//Default value
ColorConverter.ConvertFromString("#FFDDDDDD") as Color?));
}
public DionysusTextBox()
{
SetStyle();
}
}
请小心:
UIPropertyMetadata
!=FrameworkPropertyMetadata
您可以重新定义依赖项属性的默认行为,特别是,您可以定义PropertyChangedCallback
s:
public class DionysusTextBox : TextBox, IDionysusControl
{
static DionysusTextBox()
{
//For the IsReadOnly dependency property
IsReadOnlyProperty.OverrideMetadata(
//On the type DionysusTextBox
typeof(DionysusTextBox),
//Redefine default behavior
new FrameworkPropertyMetadata(
//Default value, can also omit this parameter
null,
//When IsReadOnly changed, this is executed
new PropertyChangedCallback(
(dpo, dpce) =>
{
//dpo hold the DionysusTextBox instance on which IsReachOnly changed
//dpce.NewValue hold the new value of IsReadOnly
//Run logic to set the background here, you are on the UI thread.
//Example of setting the BorderBrush from ARGB values:
var dioBox = dpo as DionysusTextBox;
//Should always be true, of course, it's just my OCD ;)
if (dioBox != null)
{
dioBox.BorderBrush =
ColorConverter.ConvertFromString("#FFDDDDDD") as Color?;
}
})));
//For the BorderBrush property
BorderBrushProperty.OverrideMetadata(
//On the type DionysusTextBox
typeof(DionysusTextBox),
//Redefine default behavior
new FrameworkPropertyMetadata(
//Default value
ColorConverter.ConvertFromString("#FFDDDDDD") as Color?));
}
public DionysusTextBox()
{
SetStyle();
}
}
请小心:
UIPropertyMetadata
!=<代码>框架属性元数据触发器只能应用于样式
。在您的情况下,styleListBoxItem
notthis
。很简单,我更改了它,不再收到错误,但该样式不起作用,有什么想法吗?我看不出您应用了该样式。@ChrisjanL尝试设置它。style=styleListBoxItem;有没有理由不在XAML中定义样式?@AndyB,该控件只是一个.cs文件。没有xaml。我希望将样式放在我的常规资源词典中,但它无权访问命名空间。触发器
只能应用于样式
。在您的情况下,styleListBoxItem
notthis
。很简单,我更改了它,不再收到错误,但该样式不起作用,有什么想法吗?我看不出您应用了该样式。@ChrisjanL尝试设置它。style=styleListBoxItem;有没有理由不在XAML中定义样式?@AndyB,该控件只是一个.cs文件。没有xaml。我希望将样式放在我的常规资源词典中,但它没有访问命名空间的权限。这对我来说很有效,只有一个问题:我正在将BackGroundProperty设置为Brush.Gray,但我遇到以下错误:“System.Drawing.SolidBrush”不是属性“Background”的有效值。。我应该将背景设置为什么?@ChrisjanL使用System.Windows.Media.brusks
。最后一个问题!在您的代码中,以下xaml是如何实现的:@chrisjann您将如何实现:ColorConverter.ConvertFromString(“#DDDDDD”)作为颜色代码>并将其分配给BorderBrush。如何在静态构造函数中分配BorderBrush?这对我来说很有效,只有一个问题:我正在将BackGroundProperty设置为Bruss.Gray,但我遇到以下错误:“System.Drawing.SolidBrush”不是属性“Background”的有效值。。我应该将背景设置为什么?@ChrisjanL使用System.Windows.Media.brusks
。最后一个问题!在您的代码中,以下xaml是如何实现的:@chrisjann您将如何实现:ColorConverter.ConvertFromString(“#DDDDDD”)作为颜色编码>并将其分配给BorderBrush。如何在静态构造函数中分配BorderBrush?