WPF ListBox ItemsSource with DataTemplate - wpf

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.

Related

how to disable a checkbox in a listbox wpf

I have a ListBox with a DataTemplate like this:
<ListBox ItemsSource="{Binding Reportes,Mode=TwoWay}" >
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel IsEnabled="{Binding PantallaActiva}">
<CheckBox FontWeight="Bold" HorizontalAlignment="Left"
Content="{Binding NORepo,Mode=TwoWay }"
IsChecked="{Binding Path=EmitirReporte,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock FontSize="9" Foreground="Gray" TextWrapping="Wrap" Text="{Binding DSRepo,Mode=TwoWay}" MaxWidth="140" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The property "PantallaActiva" is a true/false property in my ViewModel that is set to False when some process starts. I need to disable the checkBoxes when that process starts. My problem is that never happens the checkboxes or the stack panel ,can`t see the PantallaActiva property or any other property in my viewModel, they only see the properties in the items collection Reportes[i] and that's, I guess, my problem, so how do I have to do the binding ?
thanks in advance
If I understand your question correctly, the PantallaActiva property isn't part of your Reporte class (the elements in your collection). It's probably a property on your view model instead, right?
The scope of your ItemTemplate is bound to the type Reporte since you bound that collection as the ItemsSource. That's why XAML binding cannot find the property on that type. You need to bind the IsEnabled property to the property on your ViewModel.
<CheckBox IsEnabled="{Binding Path=DataContext.PantallaActiva, RelativeSource={RelativeSource AncestorType=MyControlType}}" .../>
For this to work, you need to set MyControlType to an element up your visual tree whose DataContext is bound to your view model. (probably the parent of your ListBox)
It would be helpful to see the code in your view model.
replace with this. You only need to assign value as false, as by default, it was true.
IsChecked="{Binding Path=EmitirReporte,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,Value=False}"/>

SelectedItem of ListBox with DataTemplate

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

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: ComboBox with selecteditem set make not use of SelectedIndex=0?

Why is the first element in my combobox popup menu not shown in the selected item area of
my combobox , when I use the SelectedItem binding? Without that it is showing up ?? Using
the same code selecteditem + selectedindex that is no problem!
<ComboBox
ItemsSource="{Binding SchoolclassSubjectViewModels}"
SelectedItem="{Binding SelectedSchoolclassSubjectViewModel}"
SelectedIndex="0"
Height="23"
HorizontalAlignment="Left"
Margin="375,13,0,0"
VerticalAlignment="Top"
Width="151">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding SchoolclassName}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding SubjectName}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Well as workaround I used:
SchoolclassSubjectViewModels.Add(schoolclassSubjectVM);
SelectedSchoolclassSubjectViewModel = schoolclassSubjectVM;
and this:
SelectedItem="{Binding SelectedSchoolclassSubjectViewModel,Mode=TwoWay}"
but I would prefer the xaml only way as it should really work.
It is because the reference inside your ItemsSource collection is not the same as the one in your SelectedItem property. I would venture to guess that you are using one object context to query your database for the list of SchoolclassSubject objects which the ItemsSource is bound to, but another context to query the actual data item to which you bind the SelectedItem. Even though the list contains a reference which represents the value held by your object, it is not really the same reference, but a separate instance of the same data.
There are ways to solve this issue, most of them involve using the SelectedValuePath and SelectedValue instead of the SelectedItem properties, but the concrete solution would be different depending on your particular ORM.

How to bind items of a TabControl to an observable collection in wpf?

What is the simplest example of binding the items of a TabControl to an ObservableCollection?
Each tab's content will have unique data, and indeed this data will have observableCollections of its own bound to the items components.
Currently I have a user control, which I would like to set as the content of each tab as soon as it is created. I also need to dynamically set the datacontext of this new user control when the tab is created. So, essentially, I would like the tabcontrol's observablecollection contain modelviews that map to the data in each tab.
On top of that, I need to do all this without violating MVVM in WPF! Any help?
Much appreciated!
Basic example :
<Window.Resources>
<DataTemplate x:Key="templateForTheContent" DataType="{x:Type vm:TheViewModelType}">
<v:YourUserControl/>
</DataTemplate>
<DataTemplate x:Key="templateForTheHeader" DataType="{x:Type vm:TheViewModelType}">
<TextBlock Text="{Binding ThePropertyToDisplayInTheHeader}"/>
</DataTemplate>
</Window.Resources>
...
<TabControl ItemsSource="{Binding YourCollection}"
ContentTemplate="{StaticResource templateForTheContent}"
ItemTemplate="{StaticResource templateForTheHeader}">
</TabControl>

Resources