2012-03-13 24 views
0

を置くために、私はそうのようなのItemsControl内のTextBoxの一覧を生成しています:は動的にテキストボックスと入力バインディングを生成:どこコマンド

<ItemsControl ItemsSource="{Binding CurrentCandidate.Properties}"> 
<ItemsControl.ItemTemplate> 
    <DataTemplate> 
    <DockPanel> 
     <TextBlock DockPanel.Dock="Top" Text="{Binding DisplayName}" /> 
     <Border DockPanel.Dock="Top"> 
     <TextBox Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}"> 
      <TextBox.InputBindings> 
      <KeyBinding Command="{?}" CommandParameter={?} Key="PgUp" /> 
      <KeyBinding Command="{?}" CommandParameter={?} Key="Enter" /> 
      </TextBox.InputBindings> 
    </TextBox> 
     </Border> 
    </DockPanel> 
    </DataTemplate> 
</ItemsControl.ItemTemplate> 
</ItemsControl> 

私はで利用可能です。コマンドにアクセスしたい入力バインディングの中でViewModel-Levelは、ItemsSourceと同じレベルにあります。これは利用できないプロパティにアクセスするにはHTML-隠れ場のような回避策です

<TextBlock x:Name="tbCurrentCandidate" 
       Tag="{Binding Path=CurrentCandidate}" 
       Visibility="Hidden"></TextBlock> 
    <TextBlock x:Name="tbReset" 
       Tag="{Binding Path=ResetInputCommand}" 
       Visibility="Hidden"></TextBlock> 
    <TextBlock x:Name="tbValidate" 
       Tag="{Binding Path=ValidateCommand}" 
       Visibility="Hidden"></TextBlock> 
<TextBox.InputBindings> 
    <KeyBinding Command="{Binding ElementName=tbReset, Path=Tag}" 
    CommandParameter="{Binding ElementName=tbCurrentCandidate, Path=Tag}" 
    Key="PgUp" /> 
    <KeyBinding Command="{Binding ElementName=tbValidate, Path=Tag}" 
    CommandParameter="{Binding ElementName=tbCurrentCandidate, Path=Tag}" 
    Key="Enter" /> 
</TextBox.InputBindings> 

:ItemTemplateに内からそれにアクセスするために、私は今まで次の回避策を使用しました私はそれらを必要とするが、私が思うところよりも良い方法がなければならない...

私を助けることができる誰も:フィーリングが;-)少し少ない惨め

+1

'{Binding RelativeSource = {RelativeSource FindAncestor、AncestorType = {x:Type ItemsControl}}、Path = Name}'のようなものを使ってみましたか? ItemsControlは、目的のコマンドを含むDataContextを持つ親コントロールタイプで置き換えます。 –

+0

ええ、それは私が働いて見つけた1つの事です:{RelativeSource FindAncestor、AncestorType = {x:Type UserControl}}、Path = DataContext.ResetInputCommand}。それは本当に美しいわけではありませんが、それは動作します –

+0

まだ他の応答はありません:あなたは1としてこれを投稿することができますので、私は答えとしてマークすることができます –

答えて

0

使用何かが必要なコマンドを含むDataContextを持つ親コントロールの種類によってItemsControlを交換してください。

0

なぜ不幸のその部分を作るために抱きしめあなたのベースビューではありませんm odelクラスには、ビューモデルの基底となる親プロパティがあります。次に、XAMLでParentにバインドするだけです.Command。

public class ViewModelBase : INotifyPropertyChanged 
{ 
    /// <summary> 
    /// The model's parent item. 
    /// </summary> 
    protected ViewModelBase _parent; 

    /// <summary> 
    /// Default Constructor. 
    /// </summary> 
    /// <param name="parent">Optional Parameter, The model's parent model. </param> 
    public ViewModelBase(ViewModelBase parent = null) 
    { 
     _parent = parent; 
    } 

    /// <summary> 
    /// The model's parent model. 
    /// </summary> 
    public ViewModelBase Parent 
    { 
     get { return _parent; } 
    } 

    //All other common properties and methods. 
} 

XAMLのバインディングは動的なので、親の具体的な型である必要はないためです。

{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=Name} 

よう

+0

ヒントをありがとうが、私はViewModelsをナビゲーションプロパティ。 –

関連する問題