2012-09-04 192 views
1

只是使用不同类型的绑定,并将属性绑定到另一个属性的自定义控件的依赖属性。自定义控件依赖属性绑定到属性

XAML:

<UserControl x:Class="BrickBreaker.Brick" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
d:DesignHeight="20" d:DesignWidth="50" > 
<Rectangle Width="50" Height="20" RadiusX="3" RadiusY="3" Stroke="Black" Fill="{Binding BrickFill, Mode=TwoWay}" /> 

代码背后:

public partial class Brick : UserControl 
{ 
    public Brick() 
    { 
     InitializeComponent(); 

    } 

    public Brush BrickFill 
    { 
     get { return (Brush)GetValue(BrickFillProperty); } 
     set { SetValue(BrickFillProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for BrickFill. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty BrickFillProperty = 
     DependencyProperty.Register("BrickFill", typeof(Brush), typeof(Brick), null); 



} 

在实行MainWindow.xaml

<UserControl x:Class="BrickBreaker.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:BrickBreaker="clr-namespace:BrickBreaker" mc:Ignorable="d" 
d:DesignHeight="300" d:DesignWidth="400"> 

<Grid x:Name="LayoutRoot" Background="White"> 
    <BrickBreaker:Brick Margin="100,100,0,0" BrickFill="Azure"/> 
</Grid> 

基本上我想要将矩形填充属性绑定到后面代码中的依赖属性。

谢谢。

Steve

回答

1

什么是确切的问题?组用户控件的的DataContext到代码隐藏,如:

<UserControl DataContext="{Binding RelativeSource={RelativeSource Self}}"> 

</UserControl> 
0
缺少

此:的DataContext = “{结合的RelativeSource = {的RelativeSource自}}”

相关问题