【发布时间】:2014-05-01 00:32:05
【问题描述】:
我有一个 ItemsControl 基本上是一组组合框、文本框和按钮:
底部的 XAML 都在 ItemsControl 中:(按钮有问题,最后一个元素)
<ItemsControl Grid.Row="2"
ItemsSource="{Binding CommandLinesOc}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Content="Send Command:"
HorizontalAlignment="Right"
Grid.Row="1" />
<ComboBox Grid.Column="1"
Grid.Row="1"
ItemsSource="{Binding ChromaCommandsCvs.View}"
SelectedItem="{Binding SelectedChromaCommand, UpdateSourceTrigger=PropertyChanged}" />
<Label Grid.Column="2"
Grid.Row="1"
Content="Parameter:"
HorizontalAlignment="Right" />
<TextBox Grid.Column="3"
Grid.Row="1"
Text="{Binding Parameter, UpdateSourceTrigger=PropertyChanged}" />
<Button Grid.Column="4"
DataContext="ChromaGUI.MainWindow"
Grid.Row="1"
Content="Send"
HorizontalAlignment="Center"
FontSize="15"
Command="{Binding RelativeSource=
{RelativeSource Mode=FindAncestor,
AncestorType={x:Type Window}},
Path=DataContext.SendMessageCommand}"
CommandParameter="{Binding FullCommandString}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
如您所见,我已将按钮元素绑定到中继命令。我不得不使用主窗口相对源,因为 ItemsControl 将数据上下文设置为 CommandLinesOc 中的项目。
一切正常 - 问题是 CommandParameter 似乎继承了相同的相对源上下文,因为我无法获取 FullCommandString(或可观察集合中项目之一的任何其他属性,如 Parameter 或 SelectedChromaCommand)解决任何事情。所以我的问题是,我可以(以及如何)将 CommandParameter 的数据上下文设置为 ItemsControl 给我的内容,同时将 Command 的上下文保持为父窗口?
【问题讨论】:
-
按钮定义中的这一行是什么:
DataContext="ChromaGUI.MainWindow"? -
CommandParameter 的数据上下文是这个 (
ChromaGUI.MainWindow) 是什么 -
这就是问题所在,它是在尝试获取中继命令上下文时遗留下来的
标签: c# wpf xaml itemscontrol relaycommand