2012-04-18 6 views
5

私は簡単なデモを作成して、バインド可能なユーザーコントロールを作成する方法を学習しています。私が知りたいのですが、私は、ユーザーが使用できるようにするために何をすべきかが必要ですされカスタムXAMLユーザーコントロールをバインド可能にするにはどうすればよいですか。

<UserControl x:Class="Example.ExampleHRControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid> 
     <TextBlock x:Name="textFirstName"></TextBlock> 
     <TextBlock x:Name="textLastName"></TextBlock> 
    </Grid> 
</UserControl> 

class Person 
{ 
    public string firstName; 
    public string lastName;  
    public Person(string first, string last) 
    { 
     firstName = first; 
     lastName = last; 
    } 
} 

、非常にシンプルなユーザーコントロール:私は、単純なクラスを作成しました通常の制御のようなコンテキストでの制御。

<local:ExampleHRControl x:Name="Hr1"></local:ExampleHRControl> 

をし、私は背後にあるコードを、それに対処し、値を追加することができます:私はMainWindowにこれを追加することができます

Hr1.textFirstName.Text = "John"; 
Hr1.textLasttName.Text = "Doe"; 

私はクラスPersonのインスタンスを作成することができることを好むだろうメインウィンドウのコントロールをPersonクラスにバインドするだけです。

答えて

5

この作業を行うには、いくつか必要なことがあります。あなたのコードビハインドで

、あなたはあなたのコントロールが知りたいPersonオブジェクトのための依存関係プロパティの追加:あなたのXAMLで

public static readonly DependencyProperty PersonProperty = 
          DependencyProperty.Register("Person", typeof(Person), 
                 typeof(ExampleHRControl)); 

    public Person Person 
    { 
     get { return (Person)GetValue(PersonProperty); } 
     set { SetValue(PersonProperty, value); } 
    } 

を、あなたのデータコンテキストとしてあなたのコードビハインド設定し、追加しますあなたの個人オブジェクトへの結合:個人プロパティが設定されるたびに

<UserControl x:Class="Example.ExampleHRControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300" 
     x:Name="This"> 
    <Grid> 
     <TextBlock x:Name="{Binding Path=Person.FirstName, ElementName=This}"/> 
     <TextBlock x:Name="{Binding Path=Person.LastName, ElementName=This}"/> 
    </Grid> 
</UserControl> 

今、あなたのコントロールは、人に関連付けられている姓と名で自身を更新します。

+1

特にこの 'UserControl'が' ContentControl'である場合、 'DataContext'を変更しない方が有益でしょう。これに対する簡単な解決策は、ユーザコントロールに名前を付けて、それを 'ElementName'を介してバインディングで参照することです。 – user7116

+0

6文字の変数が正しい。これを見てください[説明](http://www.scottlogic.co.uk/blog/colin/2012/02/a-simple-pattern-for-creating-re-useable-usercontrols-in-wpf-silverlight /) その理由のために。 – LPL

+0

私は上記のオリジナルの投稿で6桁の文字の変更を推奨しました。 – Curtis

2

Dependancy Propertyと呼ばれるものをxamlからバインドできます。
あなたはそれを結合するか、または単に

<local:YourControlName FirstName="john"/> 

<local:YourControlName FirstName="{Binding MyFirstName}"/> 
    それを使用してXAMLでそれを使用することができ、フィールド

    public static readonly DependencyProperty FirstNameProperty = 
        DependencyProperty.Register(
        "FirstName", typeof(Strin), 
    

    プロパティを2-作成

    public String FirstName 
    { 
        get { return (String)GetValue(FirstNameProperty); } 
        set { SetValue(FirstNameProperty, value); } 
    } 
    

    3- 1-作成

  • Resharperを使用すると、きれいなコードを作成し、非常に強力なIntelliSense
+0

ありがとう、それは私が逃したもののように見えます。私は一緒にそれを置くために働くでしょう。 –

+0

他にも1つのプロパティではなくコントロール全体をバインドできますか? –

関連する問題