0

Here is a portion of my wpf-xaml code :


<ListBox x:Name="TestJobSuiteListBox" Grid.Row="1" ItemsSource="{Binding AvailableJobs}" MouseRightButtonDown="TestJobSuiteListBox_OnMouseRightButtonDown">
    <ListBox.ItemTemplate>
        <HierarchicalDataTemplate>
            <ListBoxItem Content="{Binding Name}"/>
        </HierarchicalDataTemplate>
    </ListBox.ItemTemplate>                            
</ListBox>

I would like to add another listboxitem to that ListBox and I dont want it to be visible before you rightclick on the listbox. It also should not be bound to the "AvailableJobs" property.

Something like this :


<ListBox x:Name="TestJobSuiteListBox" Grid.Row="1" ItemsSource="{Binding AvailableJobs}" MouseRightButtonDown="TestJobSuiteListBox_OnMouseRightButtonDown">
    <ListBox.ItemTemplate>
        <HierarchicalDataTemplate>
            <ListBoxItem Content="{Binding Name}"/>
        </HierarchicalDataTemplate>
    </ListBox.ItemTemplate>
    <ListBoxItem x:Name="AddJobbListBoxItem" Visibility="Hidden"></ListBoxItem>
</ListBox>

This doesn't work, because of "itemsource must be empty problem"

Anyone have a good Idea of how I could do it ?

I don't need help with the visibility/rightclick functionality.

Thanks in advance, I hope the problem is understandable.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • The 'something like this : ' box is empty. (Fixed) – Michel Keijzers Jul 22 '14 at 12:00
  • *Anyone have a good Idea of how I could do it?* Yes... you can learn how to do it properly. Your code is so wrong that I can't even begin to help you. This website is not here to teach users how to learn a language. You should do that yourself, from tutorials online. Start with the [Data Binding Overview](http://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx) page on MSDN. – Sheridan Jul 22 '14 at 13:43

2 Answers2

0

You can put listbox to stackPanel, and disable it's scrolling. Then add that one element also to the stackPanel under the listbox, and finally add that stackPanel to scrollViewer. Then you have listbox with AddButton.

<ScrollViewer>
    <StackPanel>
         <ListBox ItemsSource="{Binding AvailableJobs}"
                  ScrollViewer.VerticalScrollBarVisibility="Disabled">
         <!-- ...  -->
         </ListBox>
         <Button x:Name="AddJobbButton" Visibility="Collapsed" />
    </StackPanel>
</ScrollViewer>

Notice that if you have lots of items in listbox, there might be some performance problems, because I'm not sure that listbox's virtualizing is working correctly if it's in stackPanel.

EDIT: of course you have to listen mouse events and set then button's visibility to visible and so on...

user3777939
  • 381
  • 2
  • 12
0

I think you have to use ItemTemplateSelector to achieve this functionality. You can create different DataTemplate as per your requirement in the Resources section and bind properly in the xaml. Check the answer here, it will give you the idea about the approach. Please refer this examlpe also. Hope this will help.

Community
  • 1
  • 1
Debasis
  • 408
  • 1
  • 3
  • 12