【发布时间】:2012-03-13 13:32:06
【问题描述】:
我正在像这样在 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 级别可用的命令,与 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-Hidden-Field 的解决方法,可以访问我需要的地方不可用的属性,但我想一定有比这更好的方法...
任何可以帮助我的人:因为让那段痛苦变得不那么痛苦而感到被拥抱;-)
【问题讨论】:
-
您尝试过使用
{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=Name}之类的东西吗?将 ItemsControl 替换为具有包含所需命令的 DataContext 的父控件类型。 -
是的,这是我发现的一件事:{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path =DataContext.ResetInputCommand}。它不是很漂亮,但它确实有效
-
由于还没有其他回复:您可以将其发布为一个,所以我可以将其标记为答案