SelectedItem of ListBox with DataTemplate - wpf

I have a Listbox with DataTemplate. The DataTemplate contains UserControl element. If I clicked on the UserControl element the Item, which contains this UserControl does not become the SelectedItem of the ListBox. If I clicked anywhere in the area of item except it's UserControl, it is selected.
How can I select the item of ListBoxby clicking on the area belongs item's UserControl?
<ListBox ItemsSource="{Binding ListOfQuestion, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
Grid.Row="1"
HorizontalContentAlignment="Stretch"
x:Name="ListOfQuestion"
SelectedItem="{Binding SelectedQuestion, Mode=TwoWay}">
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<local:usc_QuestionEdit x:Name="QuestionList"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Sounds like the user control is catching the mouse click and not the listbox. That is why it doesn't get selected. If you are using code-behind, you can use the events with the Preview prefix to catch the click higher in the control hierarchy.
To learn more about this events read about Routed Events on MSDN
Update: This post seems to have the answer you are looking for.
Hope this helps

Related

WPF Listbox Selection change not firing on Item click

In my wpf application when I select listboxItem, SelectionChanged event of listbox is not firing. However event fires when I click on outer margin.
have a look at below snap.
so basically, when I click on section inside Red border(Right Image), Selection change event is not firing, but when I click on outer border (White color part), selection change fires.
While searching about issue, I am not sure but I found it may be issue due to event tunneling. However I have only a bit of knowledge about tunneling yet.
So can any one please help me how this can get working so that selection change fire when I click on listboxitem (Red section)
let me if I need to further clear question.
I am also putting listbox code here
<ListBox x:Name="Listbox1" SelectionChanged="listBox1_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem Margin="10" Content="{Binding Name}" Height="25"
BorderBrush="#FF404040" BorderThickness="0,0.25" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Thanks in anticipation
I can't think of a reason why you would want to have a ListBoxItem inside the DataTemplate of a ItemTemplate. The ListBoxItems are genarated automatically for each element of the ListBox and whatever you have in your DataTemplate will be used as the content of that ListBoxItem So in your case you end up having a ListBoxItem inside a ListBoxItem. This might be the cause.
Try it this way:
<ListBox x:Name="Listbox1" SelectionChanged="listBox1_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Margin="10"
Content="{Binding Name}"
Height="25"
BorderBrush="#FF404040"
BorderThickness="0,0.25" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

ListBox is selecting more than one item when it is being dragged

I have a ListBox with RadioButton inside it of the same GroupName.
Like so:
<ListBox x:Name="AnswerListBox" DataContext="{Binding .}" ItemsSource="{Binding Answers}" Width="auto" HorizontalAlignment="Center" >
<ListBox.ItemTemplate>
<DataTemplate>
<local:spriteToggleButton DataContext="{Binding .}" HorizontalAlignment="Center" Text="{Binding text}" Selected="{Binding selected}" Sprites="{Binding Path=DataContext.UISprites, ElementName=questionField}" IsChecked="{Binding selected, Mode=TwoWay}" GroupName="{Binding Path=DataContext.QuestionTitle, ElementName=questionField}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When I click on a RadioButton it will unselect another in that group, this is fine.
But when I do a swipe down on the ListBox it will select more than one RadioButton which is an unwanted behavior.
There might be more than one way of changing that behavior.
I don't need it to scroll up and down the list as I will only show as many items as will fit on screen. so maybe that behavior can be disabled.
Maybe a callback could incur when the ListBox is scrolled and something could disable selection when dragging in being done.
the DataContext to the listbox is of List<> so is there a way to change the setter of my Selected variable in my List<> so that it unsets the other values in that list.
Set ScrollViewer.VerticalScrollBarVisibility to "Disabled" on the ListBox element.
In the SelectionChanged event, set the SelectedIndex to -1
You could clear all the other values in the ItemsSource in the SelectionChanged event.

WPF Binding To Child Control

I've got a TabControl which contains a nested ListView. The ListView is bound to the selected item in the parent TabControl. This works great in that switching tab displays the child elements in the ListView. What I can't figure out, is how to bind to the ListView's SelectedItem from outside of the Menu UserControl.
i.e.
<TabControl x:Name="Parent">
<TabControl.ContentTemplate>
<DataTemplate>
<ListView x:Name="Child"
ItemsSource="{Binding Path=SelectedItem.Tabs, ElementName=Parent}"/>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
<ItemsControl ItemsSource="{Binding Path=SelectedItem.Controls, ElementName=Child}">
<ItemsControl.ItemTemplate>
<DataTemplate>
... controls go here ...
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I'm using M-V-VM so don't want to do my binding in code ideally - I'm sure it's possible, just can't figure it out :)
In general, if you need a property on a higher level you could move the property to the ViewModel that is bound to the higher level.
So, if I understand correctly, I would move the property of the ViewModel that is bound to the SelectedItem to the VM of the TabControl.
Does this make sense?

WPF ListBox ItemsSource with DataTemplate

I have a ListBox with an ItemsSource pointing to a static variable, and a DataTemplate for the ListBox's ItemTemplate that should be displaying the Description property of the variable the ItemsSource points to
<ListBox x:Name="classificationTypeListBox"
ItemsSource="{x:Static h:AmbientHighlightingStyleRegistry.Instance}"
SelectedIndex="0" Foreground="Black">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=(Description)}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I can put a break point on my application and view the ListBox. The ItemsSource does point to the variable I want and it looks like the ListBox is trying to display all the values because I can click and scroll down the it. However, no text is getting displayed so you can't actually tell what you're clicking. Also, while the break point is on, it says that the list box contains 0 items, maybe it should because I'm binding it, not sure. Any suggestions?
<TextBlock Text="{Binding Path=(Description)}" />
Why do you have parens in there? This syntax causes WPF to try to bind to an attached property, which is not what you want to do.

Command Binding in UserControl used as ListBox.ItemTemplate

I have a Listbox with a UserControl as the DataTemplate. This UserControl has a Button to remove that item from the list.
<ListBox x:Name="FileList" ItemsSource="{Binding Files}" >
<ListBox.ItemTemplate>
<DataTemplate>
<Views:FileItem/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The ItemsSource is defined as:
ObservableCollection<FileViewModel> m_fileViews = new ObservableCollection<FileViewModel>();
Here is the UserControl simplified:
<UserControl x:Class="Views.FileItem">
<Canvas x:Name="LayoutRoot">
<TextBlock x:Name="FileName" Text="{Binding FileName}" />
<Button Content="Remove"/>
</Canvas>
</UserControl>
When the user clicks the Remove button, it should remove this item from the ObservableCollection.
The problem is, the DataContext for each ListBoxItem is a different ViewModel than the ViewModel that holds the ObservableCollection.
I'm not sure how to bind the Remove button to an ICommand in the "parent" ViewModel. Any help would be appreciated. Thanks so much.
I would bind the button to an ICommand in the UserControl's ViewModel, then send a message across to the parent ViewModel using loosely-coupled messaging (available in most Mvvm frameworks, like MvvmFoundation)
Let the parent VM register for the 'remove me' message, and update the ObservableCollection accordingly...
Hope this helps :)

Resources