[英]WPF - Pass parameter to relaycommand within ItemsControl
I've got an ItemsControl for basically an array of comboboxes, textboxes and buttons: 我有一个ItemsControl,基本上是一个组合框,文本框和按钮的数组:
The XAML for that bottom section is all within the ItemsControl: (Having a problem with the button, the last element) 该底部的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>
As you can see, I've got the button element bound to a relay command. 如您所见,我已经将按钮元素绑定到了中继命令。 I had to use the main window relative source since the ItemsControl is setting the data context to an item in CommandLinesOc.
我必须使用主窗口的相对源,因为ItemsControl将数据上下文设置为CommandLinesOc中的项目。
That all works fine - the problem is that CommandParameter seems to be inheriting the same relative source context, as I can't get FullCommandString (or any other property of one of the items in the observable collection, like Parameter or SelectedChromaCommand) to resolve to anything. 一切正常-问题是CommandParameter似乎继承了相同的相对源上下文,因为我无法获取FullCommandString(或可观察集合中项之一的任何其他属性,例如Parameter或SelectedChromaCommand)来解析为任何东西。 So my question is, can I (and how do I) set the data context for the CommandParameter to be what the ItemsControl is giving me, while keeping the context for the Command the parent window?
所以我的问题是,我可以(以及如何)将CommandParameter的数据上下文设置为ItemsControl给我的内容,同时将Command的上下文保留在父窗口中吗?
Currently, you have the button DataContext
set to a string "ChromaGUI.MainWindow" : 当前,您已将按钮
DataContext
设置为字符串“ ChromaGUI.MainWindow”:
<Button DataContext="ChromaGUI.MainWindow"
.......
Command="{Binding RelativeSource=
{RelativeSource Mode=FindAncestor,
AncestorType={x:Type Window}},
Path=DataContext.SendMessageCommand}"
CommandParameter="{Binding FullCommandString}"/>
String doesn't have property FullCommandString
, so your command parameter binding will fail. String没有属性
FullCommandString
,因此命令参数绑定将失败。 Simply remove DataContext
setting in the button to make it use default DataContext
which is corresponding item in the ItemsSource
: 只需删除按钮中的
DataContext
设置即可使其使用默认的DataContext
,它是ItemsSource
对应项:
<Button
.......
Command="{Binding RelativeSource=
{RelativeSource Mode=FindAncestor,
AncestorType={x:Type Window}},
Path=DataContext.SendMessageCommand}"
CommandParameter="{Binding FullCommandString}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.