C# WPF-将参数传递给ItemsControl中的relaycommand
我有一个ItemsControl,基本上是一组组合框、文本框和按钮: 底部部分的XAML都在ItemsControl中:(最后一个元素button有问题)C# WPF-将参数传递给ItemsControl中的relaycommand,c#,wpf,xaml,itemscontrol,relaycommand,C#,Wpf,Xaml,Itemscontrol,Relaycommand,我有一个ItemsControl,基本上是一组组合框、文本框和按钮: 底部部分的XAML都在ItemsControl中:(最后一个元素button有问题) 如您所见,我已经将button元素绑定到一个中继命令。我不得不使用主窗口相对源,因为ItemsControl正在将数据上下文设置为CommandLinesOc中的一个项 一切正常-问题是CommandParameter似乎继承了相同的相对源上下文,因为我无法获取FullCommandString(或可观察集合中某个项的任何其他属性,如P
如您所见,我已经将button元素绑定到一个中继命令。我不得不使用主窗口相对源,因为ItemsControl正在将数据上下文设置为CommandLinesOc中的一个项
一切正常-问题是CommandParameter似乎继承了相同的相对源上下文,因为我无法获取FullCommandString(或可观察集合中某个项的任何其他属性,如Parameter或SelectedChromaCommand)来解析任何内容。因此,我的问题是,我是否可以(以及如何)将CommandParameter的数据上下文设置为ItemsControl提供给我的内容,同时将命令的上下文保留在父窗口中 当前,您将按钮
DataContext
设置为字符串“ChromaGUI.MainWindow”:
按钮定义中的这一行是什么:
DataContext=“ChromaGUI.MainWindow”
?CommandParameter的DataContext是什么(ChromaGUI.MainWindow
),这就是问题所在,它是在尝试获取中继命令上下文时遗留下来的,谢谢。我试图让中继命令上下文正常工作时留下的愚蠢的遗留问题。
<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>
<Button DataContext="ChromaGUI.MainWindow"
.......
Command="{Binding RelativeSource=
{RelativeSource Mode=FindAncestor,
AncestorType={x:Type Window}},
Path=DataContext.SendMessageCommand}"
CommandParameter="{Binding FullCommandString}"/>
<Button
.......
Command="{Binding RelativeSource=
{RelativeSource Mode=FindAncestor,
AncestorType={x:Type Window}},
Path=DataContext.SendMessageCommand}"
CommandParameter="{Binding FullCommandString}"/>