C# 完全绑定控件,而不仅仅是属性
我正在开发一个带有用户控件的WindowsPhone8应用程序 此用户控件有边框,我想创建DependencyProperty以访问此边框:C# 完全绑定控件,而不仅仅是属性,c#,wpf,silverlight,xaml,windows-phone-8,C#,Wpf,Silverlight,Xaml,Windows Phone 8,我正在开发一个带有用户控件的WindowsPhone8应用程序 此用户控件有边框,我想创建DependencyProperty以访问此边框: public partial class CustomOptionButton : UserControl { public Border OuterBorder { get { return (Border)GetValue(OuterBorderProperty);
public partial class CustomOptionButton : UserControl
{
public Border OuterBorder
{
get
{
return (Border)GetValue(OuterBorderProperty);
}
set
{
SetValue(OuterBorderProperty, value);
}
}
public readonly DependencyProperty OuterBorderProperty =
DependencyProperty.Register("OuterBorder", typeof(Border), typeof(CustomOptionButton), null);
但是我不知道如何在XAML中绑定这个属性
绑定TextBlock.Text后,我在XAML上执行了以下操作:
<TextBlock x:Name="CustomText" Text="{Binding ButtonText, ElementName=userControl}" />
但是,我怎么能对整个边境做同样的事情呢
我需要更改BorderBrush和BorderThickness以及其他属性,因此我认为我只能有一个属性,而不是三个或四个属性。在这种情况下,您需要为每种类型的单独属性创建,因为要使用一个属性边界,需要创建一个单独的控件,如下所示:
public сlass MyBorderControl : Border
{
// Your implementation of Control
}
<Canvas>
<Button Canvas.Left="50">Hello</Button>
</Canvas>
如果要创建可用于任何控件的属性,请在WPF中附加属性:
使用方法如下:
public сlass MyBorderControl : Border
{
// Your implementation of Control
}
<Canvas>
<Button Canvas.Left="50">Hello</Button>
</Canvas>
在本例中,Canvas.Left attached属性用于Button类。最好告诉我们为什么需要边框。因为可能有更好的方法来实现您正在尝试的操作。嗯,当您必须访问某些属性时,您可能需要它来更改它,不是吗?我已经用更多的细节更新了我的问题。