I’m generating a List of TextBoxes within an ItemsControl like so :
<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>
Within the input-Bindings I want to access a Command that’s available at ViewModel-Level, at the same level as the ItemsSource, that is. In Order to access it from within the ItemTemplate, I used the following workaround up to now :
<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>
This is a HTML-Hidden-Field-like workaround to access the properties that are not available where I need them, but I suppose there must be a better way than that…
Anyone who can help me out : feel hugged for making that piece of misery a bit less miserable 😉
Use something like
Replace
ItemsControlby the parent control type that has aDataContextcontaining the desired commands.