强制ItemsControl生成项目

时间:2018-08-29 09:18:27

标签: wpf itemscontrol

如何强制ItemsControl生成其项目?

我的要求是根据项目控件中按钮的位置绘制一条线

        <ItemsControl Name="icColumns"
                          ItemsSource="{Binding FixedWidthColCount}"
                          Grid.Row="0"
                          AlternationCount="5"
                          VirtualizingPanel.IsVirtualizing="True">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel Orientation="Horizontal"
                                                VirtualizingStackPanel.VirtualizationMode="Recycling" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.Template>
                    <ControlTemplate>
                        <ItemsPresenter />
                    </ControlTemplate>
                </ItemsControl.Template>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button  Name="btnLengthDefinition"
                                 FontFamily="Courier New"
                                 FontSize="16"
                                 Foreground="{StaticResource WhiteBrush}"
                                 Content="."
                                 AllowDrop="True"
                                 Tag="{Binding }"
                                 Command="{Binding Path=DataContext.AddFixedWidthDefinitionCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}"
                                 CommandParameter="{Binding Converter={StaticResource int2StringConverter}}"
                                 Click="btnLengthDefinition_Click"
                                 Drop="btnLengthDefinition_Drop" />
                        <DataTemplate.Triggers>
                            <Trigger Property="ListBox.AlternationIndex"
                                     Value="4">
                                <Setter Property="FontWeight"
                                        Value="Bold"
                                        TargetName="btnLengthDefinition" />
                                <Setter Property="Content"
                                        Value="{Binding}"
                                        TargetName="btnLengthDefinition" />
                                <Setter Property="FontSize"
                                        TargetName="btnLengthDefinition"
                                        Value="12" />
                            </Trigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

我在视图模型/视图中具有依赖项属性,以确定在用户控件加载事件中将按项目单击哪个按钮。

public void LoadMappings()
    {
        if (ColMappings != null)
        {
            foreach (var item in ColMappings)
            {
                var uiElement = (UIElement)icColumns.ItemContainerGenerator.ContainerFromItem(item);
                if (uiElement != null)
                {
                    var btn = FindUtils.FindVisualChildren<Button>(uiElement).FirstOrDefault();
                    if (btn != null)
                        btnLengthDefinition_Click(btn, null);

                }
            }
            ColMappings = null;
        }
    }

问题在于,项目控件不会在用户控件加载时呈现/生成项目。

0 个答案:

没有答案