我正在试图让一些WPF概念失效,所以我把一个简单的例子放在我想要做的事情上。我想设置一个用户控件的自定义属性,并让它由控件中的元素使用。
我一直在研究和试验,但我并没有完全理解这里的一切。任何帮助将不胜感激。
此示例的用户控件是一个简单的正方形,其中有一个圆圈:
<UserControl x:Class="CircleInSquare"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="100" Height="100" >
<Grid Background="#000000">
<Ellipse Name="Circle"
Margin="10"
Fill="?????????"
>
</Ellipse>
</Grid>
</UserControl>
它背后的VB代码:
Partial Public Class CircleInSquare
Private _CircleColor As Color
Public Property CircleColor() As Color
Get
Return _CircleColor
End Get
Set(ByVal value As Color)
_CircleColor = value
End Set
End Property
End Class
当我使用此用户控件时,如何将CircleColor应用于控件,并将其显示为Ellipse的填充颜色?更好......我可以给它一个在VS2008 Designer中显示的默认颜色吗?
所以...如果我把这些中的一个放到我的窗口XAML中这样:
<app:CircleInSquare CircleColor="Blue" />
我希望圆圈显示为蓝色(或我为该实例选择的任何其他颜色)
答案 0 :(得分:2)
很抱歉重新发布,但重新阅读后发帖,我认为你可能会更善于模板化。我在VB中附加了一些样本
Window.xaml
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:app="clr-namespace:WpfApplicationVB1"
Title="Window1" Height="300" Width="300">
<Grid>
<app:CircleInSquare Height="50" Width="50" CircleColor="Blue" SquareColor="Red" />
</Grid>
</Window>
CircleInSquare.xaml.vb
Partial Public Class CircleInSquare
Public Property CircleColor() As Brush
Get
Return GetValue(CircleColorProperty)
End Get
Set(ByVal value As Brush)
SetValue(CircleColorProperty, value)
End Set
End Property
Public Shared ReadOnly CircleColorProperty As DependencyProperty = _
DependencyProperty.Register("CircleColor", _
GetType(Brush), GetType(CircleInSquare), _
New FrameworkPropertyMetadata(Brushes.Black))
Public Property SquareColor() As Brush
Get
Return GetValue(SquareColorProperty)
End Get
Set(ByVal value As Brush)
SetValue(SquareColorProperty, value)
End Set
End Property
Public Shared ReadOnly SquareColorProperty As DependencyProperty = _
DependencyProperty.Register("SquareColor", _
GetType(Brush), GetType(CircleInSquare), _
New FrameworkPropertyMetadata(Brushes.Gray))
End Class
CircleInSquare.xaml
<UserControl x:Class="CircleInSquare"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:app="clr-namespace:WpfApplicationVB1"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Template>
<ControlTemplate>
<Border x:Name="PART_Square" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" Background="{TemplateBinding app:CircleInSquare.SquareColor}">
<Ellipse x:Name="PART_Ellipse" Height="{TemplateBinding Height}"
Width="{TemplateBinding Width}" Fill="{TemplateBinding app:CircleInSquare.CircleColor}" />
</Border>
</ControlTemplate>
</UserControl.Template>
</UserControl>
答案 1 :(得分:0)
您需要在CircleInSquare类中创建DependencyProperty。做一些谷歌搜索。像下面这样使用属性的概念称为AttachedProperties,你可能需要处理CircleColorChanged事件来做你需要的事情。
<Ellipse app:CircleInSquare.CircleColor="Blue" />
答案 2 :(得分:0)
将椭圆的DataContext设置为CircleInSquare类的实例。并确保在此类上使用实现INotifyProperychanged以使其启用属性更改。如果您需要有关Propertychange
的更多信息,请查看此链接 <Ellipse Name="Circle"
Margin="10"
Fill="{Binding Path= CircleColor}"
>
</Ellipse>
答案 3 :(得分:0)
您可以像这样设置依赖项属性:
Public Shared ReadOnly MouseOverBrushProperty As DependencyProperty = DependencyProperty.Register("MouseOverBrush", GetType(Brush), GetType(BrushableComboBox), New UIPropertyMetadata())
Public Property MouseOverBrush() As Brush
Get
Return CType(GetValue(MouseOverBrushProperty), Brush)
End Get
Set(ByVal value As Brush)
SetValue(MouseOverBrushProperty, value)
End Set
End Property
然后在你的xaml中你会做这样的事情
Background="{TemplateBinding MouseOverBrush}"
您可以在控件模板之外设置默认样式,如下所示:
<Style TargetType="{x:Type local:BrushableComboBox}">
<Setter Property="MouseOverBrush" Value="Blue" />
...
您也可以使用普通属性来执行此操作,但依赖项属性支持绑定,这使得以后的样式更容易。
HTH