WPF 2D数组绑定和Contextmenu

时间:2014-08-08 21:54:07

标签: c# wpf xaml mvvm data-binding

我在WPF中遇到commandS-BINDING问题。我尝试使用ItemsControls显示2D数组,我有以下代码:

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>

我有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; } }
}

当我单击ContextMenu的项时,命令SomeCommand正在触发。但我不能传递给这个命令参数。 ContextMenu的DataContext绑定到绑定到名为Lst的元素的DataContext的Border.Tag,现在我不能将命令参数绑定到名为IdLabel的Label的内容。

要点:

我想将MenuItem的命令绑定到ItemsControl的DataContext(元素Lst),我想将command-parameter绑定到此itemsControl项的DataContext(到IdLabel的内容)< / p>

我怎么能这样做?预先感谢!

1 个答案:

答案 0 :(得分:1)

根据您的模板和数据模型,这应该可行

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

假设PlacementTarget(Border)是您要将DataContext(Model)传递给命令的元素

如果您希望传递Model的Id属性,只需将其重写为

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