Click here to Skip to main content
15,881,710 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,I don't know why my button doesnt work,for eg I cannot click on the button ,what is wrong here?

XML
<ListBox  x:Name="listbox"
     Focusable="True"
     PreviewMouseWheel="listBox_PreviewMouseWheel"
     IsSynchronizedWithCurrentItem="True"
     ItemsSource="{Binding}"
             >


  </ListBox.Resources>
     <ListBox.ItemsPanel>
       <ItemsPanelTemplate>
         <local:Panel3D
           Loaded="OnPanel3DLoaded"
           CorX="{Binding ElementName=sliderX, Path=Value}"
           CorY="{Binding ElementName=sliderY, Path=Value}"
           CorZ="{Binding ElementName=sliderZ, Path=Value}"
           />
       </ItemsPanelTemplate>
       </ListBox.ItemsPanel>
      
           <ListBox.ItemContainerStyle>
               <Style TargetType="{x:Type ListBoxItem}">
                   <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                   <Setter Property="Width" Value="200" />
                   <Setter Property="Height" Value="200" />
                   <Setter Property="Template">
                       <Setter.Value>
                           <ControlTemplate TargetType="{x:Type ListBoxItem}">
                               <ContentPresenter ContentSource="Content" />
                           </ControlTemplate>
                       </Setter.Value>
                   </Setter>
               </Style>
           </ListBox.ItemContainerStyle>
      <ListBox.ItemTemplate>
       <DataTemplate>
         <Button x:Name="my" Onclick="DoThis"></Button>
       </DataTemplate>
     </ListBox.ItemTemplate>
   </ListBox>
Posted
Updated 24-Oct-10 19:58pm
v2
Comments
Tarun.K.S 26-Oct-10 8:47am    
any success BaharDev?

1 solution

Actually DataTemplate is used to display the items in an ItemsControl like listbox, listview or a combobox.

And this DataTemplate is applied to the ContentPresenter or ContentControl. So you a ContentPresenter is a container for holding the DataTemplate and an ItemsPresenter is a container for holding the ItemTemplate. So that's why your button won't work because its only getting displayed.
 
Share this answer
 
Comments
BaharDev 25-Oct-10 5:04am    
thanks tarun for your answer,I quite understood what u said but since i am novice in this area could you tell me how i can manipulate my program in a way that my button works??thank you so much:d
Tarun.K.S 25-Oct-10 5:52am    
ohkay i think i got it! replace onClick with only Click and then bind the button using a Tag to identify the particular and what this button will do.
for example :
<button x:Name="My" Click="DoThis" Tag="{Binding Path=MyId}"/>

you can replace this Id with the values that you have binded to the collection(preferrably ObservableCollection) to enumerate it in the listbox ie. ListBox.ItemsSource=MyIDList
and then you can access your Id or whatever that you want to bind in the function like this :
private void DoThis(object Sender,RoutedEventArgs e)
{
Button btn=(Button)sender; 'This will identify the button
int MyId=(int)cmd.Tag;
ListBox1.SelectedItem=MyID;
..
..'Do what you want here!
..
}
So now your button should work!
BaharDev 25-Oct-10 21:39pm    
thanks tarun but i think I dont need a tag,since I want to bind image to my button like this:

<Button
Click="DoThis"
Cursor="Hand"
>
<Image
Source="{Binding}"/>
</Button>
my image is appearing but the button doesnt work i mean when i move cursor to it ,it's not clickable:(
Tarun.K.S 26-Oct-10 4:26am    
hmmm lemme try this by myself.
one thing that can be done is by enclosing the button with a grid.

Tarun.K.S 26-Oct-10 6:21am    
i just made a simple WPF application and added a button to the data template and its working fine. your itemcontainerstyle is also working fine. but i am not able to understand what your ItemsPanelTemplate is doing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900