2012-03-13 52 views
0

我生成文本框的列表一個ItemsControl內,像這樣:動態生成的文本框,並輸入綁定:當把命令

<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處於同一級別,即。爲了從ItemTemplate模板內訪問它,我用以下解決方法到現在:

<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> 

這是一個HTML隱藏場般的解決方法來訪問所不具備的特性我需要他們,但我想一定有比這更好的辦法...

任何人誰可以幫我出:感覺擁抱製作那塊苦難少一點可憐的;-)

+1

您是否嘗試過使用類似於{{Binding RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type ItemsControl}},Path = Name}'?將ItemsControl替換爲具有包含所需命令的DataContext的父控件類型。 – 2012-03-13 13:45:53

+0

是的,這是我發現的一件事:{Binding RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type UserControl}},Path = DataContext.ResetInputCommand}。這不是很漂亮,但它的工作 – 2012-03-13 14:06:57

+0

因爲還沒有其他的反應:你可以發佈這一個,所以我可以將它標記爲答案 – 2012-03-14 09:30:32

回答

0

使用類似

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

由具有含有所需命令的DataContext父控制類型替換ItemsControl

0

爲什麼不在你的基本視圖m odel類有一個父屬性,它將是一個視圖模型庫。然後在您的XAML中,您可以簡單地綁定到Parent.TheCommand。

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中的綁定是動態的,所以它不需要是父級的具體類型。

+0

感謝您的提示,但我不打算通過創建進一步膨脹我的ViewModels導航屬性。 – 2012-03-13 14:18:25