如何为嵌套集合发送ItemControl项的位置

时间:2018-11-27 13:17:35

标签: c# wpf xaml datatemplate itemscontrol

我有一个绑定到 import py2neo # Some of these keyword arguments are unnecessary, as they are the default values. graph = py2neo.Graph(uri, user=username, password=password) df = graph.run("Your query goes here").to_data_frame() 的ItemControl。最深的ItemControl中的每个项目都有一个按钮。我实现了一个命令,该命令在每次单击按钮时都会触发。
我的问题是我不知道应该将哪个参数传递给执行方法,以便知道哪个Item触发了命令。意思是,如果该按钮用作“添加书”按钮,我想知道应该在哪里插入BookModel对象

ObservableCollection<ObservableCollection<BookModel>>

<UserControl.Resources>
<DataTemplate x:Key="BooksViewTemplate">
    <StackPanel>
        <TextBlock>
            <!-- This is where I bind data to the BookModel -->
        </TextBlock>
        <Button x:Name="AddNewBook"
                Command="{Binding ElementName=LayoutRoot, Path = DataContext.AddBookCommand}"
                <!-- CommandParameter=  -->
        />
    </StackPanel>
</<DataTemplate>

<DataTemplate x:Key="DataTemplate_level1">
    <StackPanel>
        <ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource BooksViewTemplate}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Vertical">
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </StackPanel>
</DataTemplate>
</UserControl.Resources>

<Grid x:Name="LayoutRoot">
<ItemsControl ItemsSource="{Binding Books}" ItemTemplate="{DynamicResource DataTemplate_level1}" DataContext="{Binding}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

1 个答案:

答案 0 :(得分:1)

您可以使用AlternationIndex属性来获取索引。只需将AlternationCount中的ItemsControl设置为int.MaxValue

<ItemsControl ItemsSource="{Binding yourcollection}" AlternationCount="2147483647">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

如果您将一个项作为CommandParameter传递并在集合中进行搜索,那么如果集合中没有没有参考重复项,该项目将起作用,否则它将失败(您将不知道出现了哪个实例)应该采取)。

对于嵌套集合,您可以通过以下方式访问索引:

<ItemsControl ItemsSource="{Binding ColOfCol}" AlternationCount="2147483647">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding }" AlternationCount="2147483647">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock>
                            <TextBlock.Text>
                                <MultiBinding Converter="{StaticResource multivalcnv}">
                                    <Binding Path='(ItemsControl.AlternationIndex)' RelativeSource="{RelativeSource AncestorType=ContentPresenter, AncestorLevel=2}"></Binding>
                                    <Binding Path='(ItemsControl.AlternationIndex)' RelativeSource="{RelativeSource AncestorType=ContentPresenter, AncestorLevel=1}"></Binding>
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

public class MultValConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values.Length == 2)
        {
            //return (values[0], values[1]); //For the ViewModel
            return (values[0], values[1]).ToString(); //For the UI example
            }
        else
            return Binding.DoNothing;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException("It's a one way converter.");
    }
}