开发者

Cannot find control in a ListView

I have this ListView:

 <ListView Grid.Row="1" x:Name="ListLinePositions" ItemsSource="{Binding Source={StaticResource CollectionViewSource}}" SelectionChanged="ListLinePositions_SelectionChanged">
        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Margin" Value="0,0,0,5"/>
                            <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}" x:Name="posTemplate">
                                    <Grid Name="GridGroupItem">
                                        <Border Name="BorderGroupItem" CornerRadius="3" 
                                        BorderThickness="1" Padding="1,1,1,1" 
                                        Focusable="True" FocusVisualStyle="{x:Null}"
                                        Background="{StaticResource {x:Static SystemColors.GradientInactiveCaptionBrushKey}}"
                                        BorderBrush="{StaticResource {x:Static SystemColors.InactiveBorderBrushKey}}" PreviewMouseDown="BorderGroupItem_PreviewMouseD开发者_开发技巧own">
                                            <Grid>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition/>
                                                    <RowDefinition/>
                                                </Grid.RowDefinitions>
                                                <Grid Name="GridHeader" Grid.Row="0" Margin="0,2,0,2">
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition/>
                                                        <ColumnDefinition Width="100"/>
                                                    </Grid.ColumnDefinitions>
                                                    <StackPanel Orientation="Horizontal" Grid.Column="0" Height="24">
                                                        <CheckBox x:Name="checkedHeader" VerticalAlignment="Center"/>
                                                        <TextBlock Text="{Binding Name.Id, Mode=OneWay}" FontWeight="Bold" Margin="3,0,3,0" VerticalAlignment="Center" />
                                                        <Image Height="24" Width="24" Source ="{Binding Name.Partition.Sport.TypeEnum, Converter={StaticResource SportTypeToImageConverter}, Mode=OneWay}" VerticalAlignment="Center" />
                                                    </StackPanel> 
                                                </Grid>
                                            </Grid>
                                        </Border>
                                    </Grid>                                        
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListView.GroupStyle>           

        <ListView.View>
            <GridView x:Name="positionGrid">
                <GridViewColumn Width="Auto" Header="check">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox x:Name="checkedPosition" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>  
            </GridView>
        </ListView.View>
    </ListView>

I need to access <CheckBox x:Name="checkedPosition" />

My ListView loads in new window. So when I try to access the the CheckBox like this

foreach (LinePosition item in this.ListLinePositions.Items)
{
      CheckBox cb = FindByName("checkedPosition",
                                (ListViewItem)this.ListLinePositions.ItemContainerGenerator.ContainerFromItem(item)) as CheckBox;    
}

I can't find anything although ListLinePositions.Items.Count > 0. But after ShowDialog for my window I get checked positions and this code works! I don't know what to do in the first case. Please, help to find solution.

I had something like that

public LineHeaderViewDocument()
        {
            InitializeComponent();
            Title = GetTitle(_lineFilter);//изм
            ListLinePositions.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
        }

void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
        {
            if (ListLinePositions.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
            {
                //...
                    foreach (LinePosition item in this.ListLinePositions.Items)
                    {
                          CheckBox cb = FindByName("checkedPosition",
                                                    (ListViewItem)this.ListLinePositions.ItemContainerGenerator.ContainerFromItem(item)) as CheckBox;    
                    }  
            }
        }

But this doesn't work too.


I find this and it works:

 //..
CheckBox cb = FindByName("checkedPosition",
(ListViewItem)this.ListLinePositions.ItemContainerGenerator.ContainerFromItem(item)) as CheckBox;

    if (cb == null)
    {
        this.ListLinePositions.ScrollIntoView(item);
        WaitFor(TimeSpan.Zero, DispatcherPriority.SystemIdle);

        cb = FindByName("checkedPosition",
                        (ListViewItem)this.ListLinePositions.ItemContainerGenerator.ContainerFromItem(item)) as CheckBox;

    }

 public static void WaitFor(TimeSpan time, DispatcherPriority priority)
        {
            DispatcherTimer timer = new DispatcherTimer(priority);
            timer.Tick += new EventHandler(OnDispatched);
            timer.Interval = time;
            DispatcherFrame dispatcherFrame = new DispatcherFrame(false);
            timer.Tag = dispatcherFrame;
            timer.Start();
            Dispatcher.PushFrame(dispatcherFrame);
        }

        public static void OnDispatched(object sender, EventArgs args)
        {
            DispatcherTimer timer = (DispatcherTimer)sender;
            timer.Tick -= new EventHandler(OnDispatched);
            timer.Stop();
            DispatcherFrame frame = (DispatcherFrame)timer.Tag;
            frame.Continue = false;
        }

May be explain why does it work?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜