2009-07-22 3 views
1

カスタムの添付プロパティのプロパティにプロパティ(Button.Background)をバインドしようとしています。WPFがカスタムの添付プロパティを見つけられないようです。

私はC#ファイル

public static class Square 
{ 
    public static readonly DependencyProperty PlayerProperty = 
     DependencyProperty.RegisterAttached("Player", typeof(Player), 
      typeof(UIElement), new FrameworkPropertyMetadata(null)); 

    public static Player GetPlayer(UIElement element) 
    { 
     return (Player)element.GetValue(PlayerProperty); 
    } 

    public static void SetPlayer(UIElement element, Player player) 
    { 
     element.SetValue(PlayerProperty, player); 
    } 

    // Other attached properties 
} 

では私のXAMLのスニペットが

<Grid Name="board" Grid.Row="1" Grid.Column="1"> 
    <Grid.Resources> 
     <Style TargetType="{x:Type Button}"> 
      <Setter Property="Height" Value="20" /> 
      <Setter Property="Width" Value="20" /> 
      <Setter Property="BorderThickness" Value="3" /> 
      <Setter Property="Background" 
       Value="{Binding Path=(l:Square.Player).Brush, Mode=OneWay}" /> 
     </Style> 
    </Grid.Resources> 
</Grid> 

これは私が取得エラーです:

は、文字列 を変換できません「(L :Square.Player).Brush 'in属性 ' Path to 'オブジェクトタイプ ' System.Windows.Prope rtyPath ' プロパティパスが無効です。 'Square' には、 'Player'という名前のパブリックプロパティがありません。 マークアップファイル内のオブジェクト 「System.Windows.Data.Binding」でのエラー 「Gobang.Gui;コンポーネント/ mainwindow.xaml」 ライン148ポジション59

しかし、プレーヤーがある添付プロパティであるため、 Square上では、上記のコードはうまくいくはずです。

また

、私はeverythin

答えて

4

あなたの添付プロパティは、UIElementではなく、OwnerとしてSquareを指定する必要があります。

public static readonly DependencyProperty PlayerProperty = 
    DependencyProperty.RegisterAttached("Player", typeof(Player), 
     typeof(Square), new FrameworkPropertyMetadata(null)); 
+0

これは、Intellisenseのツールチップを読んでいないために得られるものです。拘束力はあまり働かない(バックグラウンドは変わらない)が、それは明日見ようとするばかげたことばかりだ。ありがとう、私はこれで多くの時間を無駄にしていた。 – Jamie

-1

あなたは、あなたがそれをやっている方法で、バインディングを設定することはできません、コードタグに私の前のタグを変更する - あなたはSquareまたはPlayerのいずれかのインスタンスが必要になりますそれに結び付ける。

+0

を継承しなければならない私は広場、広場のインスタンスにバインドしようとしていないよ、私の添付プロパティの宣言がどこにあるかです。 – Jamie

0

私はそれを動作させました。 注:その読み取り専用プロパティ、ヘルパークラスはDO

から
public class Helper : DependencyObject 
{ 
    public static readonly DependencyPropertyKey IsExpandedKey = DependencyProperty.RegisterAttachedReadOnly(
     "IsExpanded", typeof(bool), typeof(Helper), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits)); 

    public static readonly DependencyProperty IsExpandedProperty = IsExpandedKey.DependencyProperty; 

    public static bool GetIsExpanded(DependencyObject d) 
    { 
     return (bool)d.GetValue(IsExpandedKey.DependencyProperty); 
    } 

    internal static void SetIsExpanded(DependencyObject d, bool value) 
    { 
     d.SetValue(IsExpandedKey, value); 
    } 
} 
関連する問題