如何在wpf中的代码中获取Listbox控件

时间:2013-06-06 06:55:30

标签: wpf vb.net

我希望代码中有ListBox个控件。我写了以下代码:

<ListBox x:Name="listbox" Grid.Column="0" SelectedIndex="0" BorderThickness="0" Style="{StaticResource myListboxStyle}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Hidden" ItemsSource="{Binding IsAsync=True}" DataContext="{Binding IsAsync=True}" ItemStringFormat="{Binding IsAsync=True}" BorderBrush="Black" IsEnabled="True">

<ListBox.ItemTemplate> 
    <DataTemplate>
        <Grid x:Name="gridEmployee" Height="90" Margin="1,0,0,1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="80" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="25" />
            </Grid.ColumnDefinitions>

        <Grid x:Name="pic" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="8,8,0,8">
            <Border Name="mask" CornerRadius="7" removed="White" />

            <Image x:Name="imgEmp" Height="50" Width="50" Stretch="Fill">
                <Image.OpacityMask>
                    <VisualBrush Visual="{Binding ElementName=mask}"/>
                </Image.OpacityMask>

                <Image.Style>
                    <Style TargetType="{x:Type Image}">
                        <Setter Property="Source" Value="{Binding Path=PicPhoto, IsAsync=True}"></Setter>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=PicPhoto, IsAsync=True}" Value="{x:Null}">
                                <Setter Property="Source" Value="/TimeAttendance_PageTransition;component/Images/blank.jpg" />
                            </DataTrigger>
                        </Style.Triggers> 
                    </Style>
                </Image.Style>
            </Image>
        </Grid>

        <StackPanel Name="spEmpData" Margin="5,6,0,0" Width="206" Grid.Column="1">
            <TextBlock Name="tbEmpName" TextBlock.FontWeight="Bold" Text="{Binding Path=Staff_Name_e, IsAsync=True}" Margin="0,6,0,0" FontSize="22" FontFamily="Browallia New">
                <TextBlock.Effect>
                    <DropShadowEffect Color="White" BlurRadius="2" Direction="280" ShadowDepth="2"/>
                </TextBlock.Effect>
            </TextBlock>

            <TextBlock Name="tbPunchDate" Text="{Binding Path=Punch_Date, IsAsync=True}" Margin="0,0,0,0" FontSize="13" Foreground="#FF545454">

                <TextBlock.Effect>
                    <DropShadowEffect Color="White" BlurRadius="2" Direction="280" ShadowDepth="2"/>
                </TextBlock.Effect>
            </TextBlock>

            <TextBlock Name="tbEmpCode" Text="{Binding Path=Emp_Code, IsAsync=True}" Margin="0,3,0,0" FontSize="13" Foreground="#FF545454">

                <TextBlock.Effect>
                    <DropShadowEffect Color="White" BlurRadius="2" Direction="280" ShadowDepth="2"/>
                </TextBlock.Effect>
            </TextBlock>
    </StackPanel>

    <Canvas Grid.Column="2">
        <Ellipse Margin="0,11,0,0" Height="18" Width="18" Opacity="1" VerticalAlignment="Top" HorizontalAlignment="Center" d:LayoutOverrides="Width">
            <Ellipse.Fill>
                <LinearGradientBrush EndPoint="0.0,0.1" StartPoint="0.0,0.0">
                    <GradientStop Color="{Binding Path=Function_Key, IsAsync=True}" Offset="1"/>
                </LinearGradientBrush>
            </Ellipse.Fill>
        </Ellipse>    
    </Canvas>   
</Grid>

    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True">
                <Setter TargetName="tbEmpName" Property="Foreground" Value="Black"/>
                <Setter TargetName="tbEmpCode" Property="Foreground" Value="Black"/>
                <Setter TargetName="tbPunchDate" Property="Foreground" Value="Black"/>
                <Setter TargetName="gridEmployee" Property="Background">

                <Setter.Value>
                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="{DynamicResource {x:Static SystemColors.GradientActiveCaptionColorKey}}" Offset="0"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </DataTemplate.Triggers>
    </DataTemplate>
</ListBox.ItemTemplate>

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

这是我的ListBox,我希望代码中的ListBox内部控件可以更改列表框项的内容。我在后面的代码中得到ListBox控件('listbox'),但我没有得到Image('imgEmp')&amp;代码背后的文本块。

显然我想要控制VB 6中的控件数组等列表框项目。我该如何实现?

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以使用VisualTreeHelper类来迭代视觉效果 看到  Link