2012-03-10 5 views
0

は私が何かを選択するために、サブフォームを開き、テキストボックス、ボタン、ボタンでのControlTemplateを持っていると、このように、テキストボックス内の項目を選択表示:ControlTemplateの2つの独立したインスタンスをウィンドウで作成する方法は?

<Window.Resources> 
    <ControlTemplate x:Key="CreateParam"> 
     <Grid> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="1*"/> 
       <ColumnDefinition Width="1*"/> 
       <ColumnDefinition Width="3*"/> 
      </Grid.ColumnDefinitions> 
      <Button Content="select" Command="{Binding ShowSpecItemViewommand}" Grid.Column="0" Margin="2"/> 
      <TextBox Margin="2" Text="{Binding Param}" Grid.Row="0" Grid.Column="1"/> 
      <TextBlock Margin="5" Text="patameter" Grid.Row="0" Grid.Column="2"/> 
     </Grid> 
    </ControlTemplate> 
    </Window.Resources> 

と私はこのようなのviewmodelでプロパティを持っています:

public string param; 
    public string Param 
    { 
     get 
     { 
      return param; 
     } 
     set 
     { 
      param = value; 
      RaisePropertyChanged("Param"); 
     } 
    } 

とは、今私がウィンドウにその制御の二つの独立したインスタンスを作成したいのですが、私は最初のインスタンスの値を選択すると、それらの両方は、私は2つのプロパティを定義し、どのようにchanged.shouldされています?それらをコントロールテンプレートにバインドできますか? 私はすべての人が私の質問を編集してくれることを願っています。

答えて

0

どのようにコントロールテンプレートを使いますか?どのテンプレートにこのテンプレートを添付しますか?あなたが持っているカスタムコントロールのテンプレートですか?既知のコントロールのテンプレートですか?

コントロールテンプレートのDataContextをどのようにインスタンス化しますか?

ControlTemplate(およびカスタムコントロール)を使用して必要なものを実装することができますが、オブジェクトのインスタンスが多数(つまり2つ以上あります)あり、ControlTemplateが正しいパラダイムである場合、方法は、DataTemplate、またはUserControlを使用する方が良いでしょう。あなたが望むものを達成するための方法は複数ありますが、以下のコードは "標準的"なソリューションと考えられています:

Say ParamはMyVMオブジェクトのプロパティです。次に、XAMLファイルは次のようになります。

<Window 
    x:Class="SO.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:so="clr-namespace:SO" 
    Height="200" Width="350" 
    Title="SO Sample" 
    > 
    <Window.Resources> 
     <DataTemplate DataType="{x:Type so:MyVM}"> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="1*"/> 
        <ColumnDefinition Width="1*"/> 
        <ColumnDefinition Width="3*"/> 
       </Grid.ColumnDefinitions> 
       <Button Content="select" Command="{Binding ShowSpecItemViewommand}" Grid.Column="0" Margin="2"/> 
       <TextBox Margin="2" Text="{Binding Param}" Grid.Row="0" Grid.Column="1"/> 
       <TextBlock Margin="5" Text="patameter" Grid.Row="0" Grid.Column="2"/> 
      </Grid> 
     </DataTemplate> 
    </Window.Resources> 

    <StackPanel> 
     <ContentControl> 
      <so:MyVM Param="1234" /> 
     </ContentControl> 
     <ContentControl> 
      <so:MyVM Param="5678" /> 
     </ContentControl>   
    </StackPanel> 

</Window> 
関連する問題