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

问题描述:

我希望代码中的Listbox控件落后。我写的代码如下:

I want Listbox controls in code behind. following code I have written,

<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>
             <ColumnDefinition Width="*"></ColumnDefinition>
             <ColumnDefinition Width="25"></ColumnDefinition>
           </Grid.ColumnDefinitions>
                        
           <Grid x:Name="pic" HorizontalAlignment="Center"                        

                                 VerticalAlignment="Center" 

                                 Margin="8,8,0,8">
             <Border Name="mask" CornerRadius="7" removed="White"></Border>
             <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>





This is my list box, I want list box internal controls in code behind for to change the content of list box items.

I get listbox control (’listbox’) in code behind but I didn’t get Image (’imgEmp’) & text blocks in code behind.



Clearly speaking I want control on list box items like Control array in VB 6.



How should I achieve this?



Thanks in advance.. :)



This is my list box, I want list box internal controls in code behind for to change the content of list box items.
I get listbox control ('listbox') in code behind but I didn't get Image ('imgEmp') & text blocks in code behind.

Clearly speaking I want control on list box items like Control array in VB 6.

How should I achieve this?

Thanks in advance.. :)

Why exactly do you need the controls in your code behind? Since you are already using binding you can easily update the controls internally used by the ListBox. If you just need to update the text of other properties of the internall controls use binding, if you need to select what control to use to render a specific item use TemplateSelectors and if you need to change the template when a certain property of your item changes use triggers to apply a different template.



Hope this helps,

Uroš
Why exactly do you need the controls in your code behind? Since you are already using binding you can easily update the controls internally used by the ListBox. If you just need to update the text of other properties of the internall controls use binding, if you need to select what control to use to render a specific item use TemplateSelectors and if you need to change the template when a certain property of your item changes use triggers to apply a different template.

Hope this helps,
Uroš


One possibility, and I am not sure this is a solution for you, is to create a user control and then can put the code behind in the user control. Then refer to this UserControl in the ItemTemplate
One possibility, and I am not sure this is a solution for you, is to create a user control and then can put the code behind in the user control. Then refer to this UserControl in the ItemTemplate