【问题标题】:How to get Listbox Controls in code behind in wpf如何在 wpf 中的代码中获取列表框控件
【发布时间】:2013-06-30 21:11:00
【问题描述】:

我想要 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 control ('listbox'),但在后面的代码中没有得到Image ('imgEmp') 和文本块。

显然,我想控制列表框项目,如 VB 6 中的 Control 数组。我应该如何实现这一点?

提前致谢。

【问题讨论】:

    标签: wpf vb.net


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-27
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      相关资源
      最近更新 更多