无法在ListView中找到控件

时间:2011-08-29 13:47:28

标签: wpf listview controls find

我有这个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_PreviewMouseDown">
                                            <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>

我需要访问<CheckBox x:Name="checkedPosition" />

我的ListView在新窗口中加载。所以当我尝试像这样访问CheckBox时

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

虽然ListLinePositions.Items.Count > 0我找不到任何东西。但在ShowDialog为我的窗口后,我得到检查位置,这个代码工作!我不知道在第一种情况下该怎么做。请帮助找到解决方案。

我有类似的东西

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

但这也行不通。

1 个答案:

答案 0 :(得分:0)

我发现了这一点并且有效:

 //..
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;
        }

可以解释为什么会起作用?