Question

I've got an ItemsControl for basically an array of comboboxes, textboxes and buttons: GUI

The XAML for that bottom section is all within the ItemsControl: (Having a problem with the button, the last element)

    <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.

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. 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?

Was it helpful?

Solution

Currently, you have the button DataContext set to a string "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. Simply remove DataContext setting in the button to make it use default DataContext which is corresponding item in the ItemsSource :

<Button 
        .......
        Command="{Binding RelativeSource=
            {RelativeSource Mode=FindAncestor, 
            AncestorType={x:Type Window}}, 
            Path=DataContext.SendMessageCommand}"
        CommandParameter="{Binding FullCommandString}"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top