【问题标题】:WPF - Pass parameter to relaycommand within ItemsControlWPF - 将参数传递给 ItemsControl 中的 relaycommand
【发布时间】: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


【解决方案1】:

目前,您已将按钮 DataContext 设置为字符串“ChromaGUI.MainWindow”:

<Button DataContext="ChromaGUI.MainWindow"
        .......
        Command="{Binding RelativeSource=
            {RelativeSource Mode=FindAncestor, 
            AncestorType={x:Type Window}}, 
            Path=DataContext.SendMessageCommand}"
        CommandParameter="{Binding FullCommandString}"/>

字符串没有属性FullCommandString,所以你的命令参数绑定会失败。只需删除按钮中的DataContext 设置,使其使用默认的DataContext,即ItemsSource 中的对应项:

<Button 
        .......
        Command="{Binding RelativeSource=
            {RelativeSource Mode=FindAncestor, 
            AncestorType={x:Type Window}}, 
            Path=DataContext.SendMessageCommand}"
        CommandParameter="{Binding FullCommandString}"/>

【讨论】:

  • 啊,谢谢。当我试图让中继命令上下文正常工作时,愚蠢的剩余物。
猜你喜欢
  • 2019-03-02
  • 1970-01-01
  • 2014-06-13
  • 2018-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-27
  • 1970-01-01
相关资源
最近更新 更多