Junior222
Junior222

Reputation: 833

WPF 2D-array binding and Contextmenu

I have a problem with commandS-BINDING In WPF. I try to display 2D-array with ItemsControls and i have the following code:

xaml:

<ItemsControl x:Name="Lst" ItemsSource="{Binding Items}" ItemTemplate="{DynamicResource DataTemplateLevel1}"/>

<DataTemplate x:Key="DataTemplateLevel1">
    <ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource FirstTemplate}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</DataTemplate>


<DataTemplate x:Key="FirstTemplate" >
    <Border BorderThickness="1" BorderBrush="Black" Margin="3, 3, 3, 3" 
            Tag="{Binding DataContext, ElementName=Lst}">
        <Border.ContextMenu>
            <ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}" Tag="{Binding Items}" >
                <MenuItem Header="Delete" Command="{Binding SomeCommand}" 
                          CommandParameter="What should i write here?" />
            </ContextMenu>
        </Border.ContextMenu>

        <Grid  Background="MediumSeaGreen"  >
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>

            <Label Grid.Row="0" Grid.Column="0" Name="IdLabel" Content="{Binding Id}"  />
            <Label Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left"     
                   Foreground="White" Margin="5, 0, 0, 0" 
                   FontSize="20" FontWeight="Heavy"  Content="{Binding Name}"   />                    
        </Grid>
    </Border>
</DataTemplate>

An i have ViewModel:

public class ViewModel 
{
    public List<List<Model>> Items
    {
        get
        {
            var lsts = new List<List<Model>>();

            for (int i = 0; i < 5; i++)
            {
                var range = new List<Model>();

                for (int j = 0; j < 5; j++)
                    range.Add(new Model{ Id = i*5 + j, Name = "Some item" });

                lsts.Add(range);
            }

            return lsts;
        }
    }

    private readonly ICommand _command = new MyCommand();

    public ICommand SomeCommand { get { return _command; } }
}

When i click ContextMenu's item the command SomeCommand is firing. But i can not pass to this command parameter. DataContext of ContextMenu is bound to Border.Tag that is bound to DataContext of element named Lst and now i can not to bind command parameter to content of Label named IdLabel.

Summary:

I want to bind MenuItem's command to ItemsControl's DataContext (of element Lst) and i want to bind command-parameter to DataContext of item of this itemsControl (to IdLabel's content)

How could i do it? Thank in advance!

Upvotes: 1

Views: 646

Answers (1)

pushpraj
pushpraj

Reputation: 13679

based on your template and data model, this should work

        <ContextMenu DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}" Tag="{Binding Tag.Items}" >
            <MenuItem Header="Delete" Command="{Binding Tag.SomeCommand}" 
                      CommandParameter="{Binding DataContext}" />
        </ContextMenu>

assuming PlacementTarget(Border) is the element whose DataContext(Model) you are trying to pass to your command

if you wish to pass the Id property of Model simply rewrite as

        <ContextMenu DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}" Tag="{Binding Tag.Items}" >
            <MenuItem Header="Delete" Command="{Binding Tag.SomeCommand}" 
                      CommandParameter="{Binding DataContext.Id}" />
        </ContextMenu>

Upvotes: 1

Related Questions