Заставить ItemsControl генерировать предметы - PullRequest
0 голосов
/ 29 августа 2018

Как мне заставить 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;
        }
    }

проблема в том, что элемент управления не отображает / генерирует элементы при загрузке пользовательского элемента управления.

...