2017-03-29 6 views
0

私はアプリケーションのデータ型とコントロールのマッピングを作成していたので、ContentPresenterとData Templatesを使用することで実現可能であることがわかりました。私はそれを成功させました。このスニペットには、System.Stringデータ型のマッピングが含まれていました。しかし、文字列のリストをバインドし、そのためのジェネリックコレクションコントロールをマップすると、文字列は、下の図(StringCollectionを参照)に示すように単純な文字列としてではなく、TextBoxとして表されることに気付きました!WPF - コレクションに特異オブジェクトのデータテンプレートを再利用するにはどうすればよいですか?

enter image description here

は今、私は、ダイナミックPropertyValueをがコレクションに割り当てられているコントロールを再利用するためには、このレバレッジを使用したいです。 TextBoxのマルチバインディングを実装するにはどうすればよいですか? PropertyValueが配列の場合は、その要素にバインドします。それ以外の場合はPropertyValue自体にバインドします。

<DataTemplate> 
    <StackPanel x:Name="ItemStackPanel" Orientation="Horizontal" Margin="8"> 
     <TextBlock Text="{Binding PropertyName}" FontSize="14" MinWidth="120" 
      Validation.ErrorTemplate="{x:Null}"> 
     </TextBlock> 
     <ContentPresenter Margin="48, 0, 0, 0" Content="{Binding PropertyValue}"> 
      <ContentPresenter.Resources> 
       <DataTemplate DataType="{x:Type System:String}"> 
        <TextBox Text="{Binding ElementName=ItemStackPanel, Path=DataContext.PropertyValue}" 
         Width="100" Validation.ErrorTemplate="{StaticResource ErrorTemplate}"> 
        </TextBox> 
       </DataTemplate> 
       <DataTemplate DataType="{x:Type dataTypes:StringCollection}"> 
        <controls:GenericCollectionControl x:Name="GenericCollectionControl" 
         ItemsSource="{Binding ElementName=ItemStackPanel, Path=DataContext.PropertyValue}" 
         Validation.ErrorTemplate="{StaticResource ErrorTemplate}"> 
        </controls:GenericCollectionControl> 
       </DataTemplate> 
      </ContentPresenter.Resources> 
     </ContentPresenter> 
    </StackPanel> 
</DataTemplate> 

---リビジョン1 ---

私はそれがIMultiValueConverterを利用して動作し、正しいバインディングを返すように成功しました。

<TextBox Width="100" Validation.ErrorTemplate="{StaticResource ErrorTemplate}"> 
    <TextBox.Text> 
     <MultiBinding Converter="{StaticResource BindingSelector}"> 
      <Binding Mode="OneWay"></Binding> 
      <Binding ElementName="ItemStackPanel" Path="DataContext.PropertyValue"></Binding> 
     </MultiBinding> 
    </TextBox.Text> 
</TextBox> 

ただし、バインディングモードが一方向でない場合は例外が発生します。これに対して、私はどのようにして双方向バインドを実現できますか?

答えて

0

ただし、バインディングモードが一方向でない場合は例外があります。これに対して、私はどのようにして双方向バインドを実現できますか?

は、第一の結合のためのパスを指定してください: ""

<TextBox Width="100" Validation.ErrorTemplate="{StaticResource ErrorTemplate}"> 
    <TextBox.Text> 
     <MultiBinding Converter="{StaticResource BindingSelector}"> 
      <Binding Path="." /> 
      <Binding ElementName="ItemStackPanel" Path="DataContext.PropertyValue"></Binding> 
     </MultiBinding> 
    </TextBox.Text> 
</TextBox> 
+0

=パス何を平均? – Xegara

+0

これは、私があなたのケースの文字列と信じているDataContext自体にバインドしていることを意味します。双方向バインディングには明示的なパスが必要です。 – mm8

関連する問題