1

さまざまな種類のバインディングをいじって、カスタム コントロールの Dependency Property を別のプロパティにバインドするプロパティを持っているだけです。

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>

基本的に、コード ビハインドで Rectangles Fill プロパティを Dependency プロパティにバインドしたいと考えています。

ありがとう。

スティーブ

4

2 に答える 2

1

正確な問題は何ですか?次のように、UserControl の DataContext をコード ビハインドに設定します。

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

</UserControl>
于 2012-09-04T15:37:50.403 に答える
0

これがありません:DataContext = "{Binding RelativeSource = {RelativeSource Self}}"

于 2012-09-05T06:05:00.383 に答える