MVVM - ListBox SelectedItem Binding Property Going Null - wpf

So i have a listbox:
<ListBox x:Name="listbox" HorizontalAlignment="Left" Margin="8,8,0,8" Width="272" BorderBrush="{x:Null}" Background="{x:Null}" Foreground="{x:Null}" ItemsSource="{Binding MenuItems}" ItemTemplate="{DynamicResource MenuItemsTemplate}" SelectionChanged="ListBox_SelectionChanged" SelectedItem="{Binding SelectedItem}">
</ListBox>
and i have this included in my viewmodel:
public ObservableCollection<MenuItem> MenuItems
{
get
{
return menuitems;
}
set
{
menuitems = value;
NotifyPropertyChanged("MenuItems");
}
}
public MenuItem SelectedItem
{
get
{
return selecteditem;
}
set
{
selecteditem = value;
NotifyPropertyChanged("SelectedItem");
}
}
and also in my viewmodel:
public void UpdateStyle()
{
ActiveHighlight = SelectedItem.HighlightColor;
ActiveShadow = SelectedItem.ShadowColor;
}
So, the objective is to call UpdateStyle() whenever selectedchanged event is fired. So in the .CS file, i call UpdateStyle().
The problem is, whenever I get into the selectionchanged event method, my ViewModel.SelectedItem is always null.
I tried debugging this to see if the binding was working correctly, and it is. When I click on an item in the listbox, the SelectedItem Set is triggered, setting the value... but somewhere inbetween that and the selected changed (In the CS File) It gets reset to Null.
Can anyone help out?
Thanks
Edit:
I thought I might shed a little more light.
1. Click on an item in the list
2. SelectedItem.Set gets triggered, ViewModel.SeletedItem gets set correctly.
3. Enter the OnSelectionChanged Event in the .CS file.
4. Enter ViewModel.UpdateStyle()
5. SelectedItem Throws a Null Exception.

Wow, found a strange issue:
<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource MainViewModelDataSource}}" d:DataContext="{d:DesignData /SampleData/MainViewModelSampleData.xaml}">
That code is generated by Expression Blend - and it was causing the issue. I erased all generated binding and just made a this.datacontext a new VM in the constructor of the XAML... now its working.
Thanks anyway, guys.

Look to see if your backing property (selecteditem) is getting set to NULL somewhere in your code.

Related

WPF ListView ScrollViewer.VerticalScrollBarVisibility Binding problem

Iam using WPF with MVVM pattern.
I have a ListView and Iam trying to do a binding with the property ScrollViewer.VerticalScrollBarVisibility , because I need to know in the ViewModel, when the scroll of the List View is Visible to do some things later. My problem is that for any reason, when the ScrollViewer.VerticalScrollBarVisibility is Visible (because there are some many items in the list) the property "MyScrollVisibility" is not being updated in the ViewModel.
Here is my code:
xaml
<ListView
ItemsSource="{Binding Items}"
ScrollViewer.VerticalScrollBarVisibility="{Binding
MyScrollVisibility, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedItem}" >
ViewModel...
public ScrollBarVisibility MyScrollVisibility
{
get
{
return _myScrollVisibility;
}
set
{
_myScrollVisibility = value;
RaisePropertiesChanged("MyScrollVisibility");
}
}
Thanks in advance for any help or idea

ItemsControl not showing items

It has been while since I worked with XAML on a regular basis and I am struggling with the basics.
I am trying to show items in an ItemsControl like so:
<DockPanel DockPanel.Dock="Left" Width="800">
<TextBlock DockPanel.Dock="Top" Text="{Binding ProfilePages.Count}"></TextBlock>
<Grid>
<ItemsControl ItemsSource="{Binding ProfilePages}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="Hello World" Height="100" Width="200" Background="AliceBlue"></TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</DockPanel>
The ViewModel is just as basic:
public class XtmProjectViewModel : NotifyingObject
{
private ViewModelCollection<XtmProfilePageViewModel, XtmProfilePage> _profilePages;
public ViewModelCollection<XtmProfilePageViewModel, XtmProfilePage> ProfilePages
{
get { return _profilePages; }
set
{
_profilePages = value;
RaisePropertyChanged(() => ProfilePages);
}
}
public ViewModelCollection<XtmSearchPageViewModel, XtmSearchPage> SearchPages { get; }
public XtmProjectViewModel(XtmProject model)
{
ProfilePages = new ViewModelCollection<XtmProfilePageViewModel, XtmProfilePage>(model.ProfilePages, s => new XtmProfilePageViewModel(s));
SearchPages = new ViewModelCollection<XtmSearchPageViewModel, XtmSearchPage>(model.SearchPages, s => new XtmSearchPageViewModel(s));
ProfilePages.CollectionChanged += ProfilePages_CollectionChanged;
}
private void ProfilePages_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
Console.WriteLine("Test");
RaisePropertyChanged(() => ProfilePages);
}
}
ViewModelCollection is a custom type which synchs with the underlying collection of models automatically. I've used this for years in all types of scenarios with no problems.
However, in the view, the items don't show up and I get a weird behavior which I cannot explain:
The text block bound to ProfilePages.Count works as expected, i.e. the number showing up is the number of items in the list.
No binding errors
The CollectionChanged event of the ProfilePages-collection is fired correctly
Also adding a RaisePropertyChanged-event for the entire collection property in the CollectionChanged event handler doesn't change the behavior
The get accessors of the ProfilePages property is called twice as expected in the previous sceanrio (firing RaisePropertyChanged)
When I edit the XAML while debugging, sometimes items show up in the ItemsControl as expected. The list of items doesn't update afterwards, however
I cannot explain the behavior and have no idea, what the issue is. I have checked the common troubles (wrong definition of ItemTemplate, missing CollectionChanged event, layout bugs causing items to render invisibly, etc. with no success).
How can this behavior be explained?
How can it be fixed?
On request of the OP, moving my comment to an answer, 15000 here we come ;)
wondering if you are inserting the objects into ProfilePages not on the UI thread.

WPF MVVM getting the textbox data to the ViewModel

I have read many questions on this and so far I've not been able to find the answer on this apparently simple issue.
I have a view model, in which is a property. In my XAML I have a TextBox with a binding to that property.
But the property never seems to change.
Here's the textbox:
<TextBox Grid.Row="1"
Grid.Column="0"
Margin="4"
Text="{Binding CharNameFromTB}" />
And the relevant code behind for the ViewModel:
private String _charNameFromTB;
String CharNameFromTB
{
get { return _charNameFromTB; }
set
{
if (!string.Equals(this._charNameFromTB, value))
{
this._charNameFromTB = value;
RaisePropertyChanged("CharNameFromTB");
}
}
}
I have put a break point on the if statement in the setter, but it never triggers. Have I missed something obvious out? I tried setting the binding mode to twoway but that didn't change anything.
It's driving me a little mad. Any help would be appreciated!
You should make the property public in order to be able to bind to it:
private String _charNameFromTB;
public String CharNameFromTB
{
get { return _charNameFromTB; }
set
{
this._charNameFromTB = value;
RaisePropertyChanged("CharNameFromTB");
}
}
Also make sure that you have set the DataContext of the TextBox or any of its parent elements to an instance of your view model class where the CharNameFromTB property is defined.
Also note that by default, the source property is set when the TextBox loses focus.
If you want to update the source property on each keystroke you should set the UpdateSourceTrigger property of the Binding to PropertyChanged:
<TextBox Grid.Row="1"
Grid.Column="0"
Margin="4"
Text="{Binding CharNameFromTB, UpdateSourceTrigger=PropertyChanged}" />

WPF ListView does not get updated when changing item DisplayMemberPath MVVM

I have WPF ListView which bind to ObservableCollection.
Here is my ListView:
<ListView
BorderBrush="#6797c8"
BorderThickness="2"
ItemsSource="{Binding Path=MainCategoriesCollection, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Category"
SelectedValuePath="MainCatID"
SelectedItem="{Binding MainCategorySelectedItem}"
SelectedIndex="{Binding MainCategorySelectedIndex, Mode=TwoWay}"
FontSize="14"/>
and this is my ItemSource:
private ObservableCollection<DataModel.MainCategories> mainCategoriesCollection;
public ObservableCollection<DataModel.MainCategories> MainCategoriesCollection
{
get
{
if (mainCategoriesCollection == null)
{
mainCategoriesCollection = new ObservableCollection<DataModel.MainCategories>();
}
return mainCategoriesCollection;
}
set
{
mainCategoriesCollection = value;
RaisePropertyChanged("MainCategoriesCollection" );
}
}
I have wired problem. When I add items or delete item from MainCategoriesCollection my ListView get updated without any problem but when I take specific item and change the item property which represent "DisplayMemberPath" I can`t see the change in the ListView. I debugged the problem and saw that the change is exists in MainCategoriesCollection but my ListView refuse to show it.
Any ideas?
Ensure the DisplayMemberPath property is on a view model with all the INotifyPropertyChanged stuff, i.e. is the property observable?

TabControl's SelectedItem gets overwritten by NewItemPlaceholder when adding tab

I'm working on a WPF TabControl whose last item is always a button to add a new tab, similar to Firefox:
The TabControl's ItemSource is bound to an ObservableCollection, and adding an item to the collection via this "+" button works very well. The only problem I'm having is that, after having clicked the "+" tab, I cannot for the life of me set the newly created (or any other existing tab) to focus, and so when a tab is added, the UI looks like this:
To explain a bit how I'm achieving this "special" tab behavior, the TabControl is templated and its NewButtonHeaderTemplate has a control (Image in my case) which calls the AddListener Command in the view-model (only relevant code is shown):
<Window x:Class="AIS2.PortListener.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ais="http://www.leica-geosystems.com/xaml"
xmlns:l="clr-namespace:AIS2.PortListener"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
DataContext="{Binding Source={StaticResource Locator}>
<Window.Resources>
<ResourceDictionary>
<DataTemplate x:Key="newTabButtonHeaderTemplate">
<Grid>
<Image Source="..\Images\add.png" Height="16" Width="16">
</Image>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<cmd:EventToCommand
Command="{Binding Source={StaticResource Locator},
Path=PortListenerVM.AddListenerCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Grid>
</DataTemplate>
<DataTemplate x:Key="newTabButtonContentTemplate"/>
<DataTemplate x:Key="itemHeaderTemplate">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
<DataTemplate x:Key="itemContentTemplate">
<l:ListenerControl></l:ListenerControl>
</DataTemplate>
<l:ItemHeaderTemplateSelector x:Key="headerTemplateSelector"
NewButtonHeaderTemplate="{StaticResource newTabButtonHeaderTemplate}"
ItemHeaderTemplate="{StaticResource itemHeaderTemplate}"/>
<l:ItemContentTemplateSelector x:Key="contentTemplateSelector"
NewButtonContentTemplate="{StaticResource newTabButtonContentTemplate}"
ItemContentTemplate="{StaticResource itemContentTemplate}"/>
</ResourceDictionary>
</Window.Resources>
<TabControl Name="MainTab" Grid.Row="2" ItemsSource="{Binding Listeners}"
ItemTemplateSelector="{StaticResource headerTemplateSelector}"
ContentTemplateSelector="{StaticResource contentTemplateSelector}"
SelectedItem="{Binding SelectedListener}">
</TabControl>
The AddListener command simply adds an item to the ObservableCollection which has for effect to update the TabControl's ItemSource and add a new tab:
private ObservableCollection<Listener> _Listeners;
public ObservableCollection<Listener> Listeners
{
get { return _Listeners; }
}
private object _SelectedListener;
public object SelectedListener
{
get { return _SelectedListener; }
set
{
_SelectedListener = value;
OnPropertyChanged("SelectedListener");
}
}
public PortListenerViewModel()
{
// Place the "+" tab at the end of the tab control
var itemsView = (IEditableCollectionView)CollectionViewSource.GetDefaultView(_Listeners);
itemsView.NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtEnd;
}
private RelayCommand _AddListenerCommand;
public RelayCommand AddListenerCommand
{
get
{
if (_AddListenerCommand == null)
_AddListenerCommand = new RelayCommand(param => this.AddListener());
return _AddListenerCommand;
}
}
public void AddListener()
{
var newListener = new TCPListener(0, "New listener");
this.Listeners.Add(newListener);
// The following two lines update the property, but the focus does not change
//this.SelectedListener = newListener;
//this.SelectedListener = this.Listeners[0];
}
But setting the SelectedListener property does not work, even though the TabControl's SelectedItem is bound to it. It must have something to do with the order in which things get updated in WPF, because if I set a breakpoint in the SelectedListener's set I can see the following happening:
this.Listeners.Add(newListener);
this.SelectedListener = newListener;
SelectedListener set gets called with correct Listener object
SelectedListener set gets called with NewItemPlaceholder object (of type MS.Internal.NamedObject according to the debugger)
Is there a way that I can work around this issue? Do I have the wrong approach?
I think you are triggering two events when you click the new tab: MouseLeftButtonDown and TabControl.SelectionChanged
I think they're both getting queued, then processing one at a time.
So your item is getting added, set as selected, and then before the re-draw occurs the SelectionChanged event occurs to change the selection to the [+] tab.
Perhaps try using the Dispatcher to set the SelectedItem so it occurs after the TabControl changes it's selection. Or make it so if the user tries to switch to the NewTab, it cancels the SelectionChanged event so the selected tab doesn't actually change (of course, the SelectedTab will be your NewItem since the MouseDown event will have occurred)
When I did something like this in the past, I actually overwrote the TabControl Template to create the AddTab button as a Button, not as a TabItem. I want to suggest doing that instead of using the NewItemPlaceholder in the first place, but I've never tried working with the NewItemPlaceholder so don't really know if it's better or worse than overwriting the Template.
Take a look at this post regarding sentinel objects: WPF Sentinel objects and how to check for an internal type
There are several ways to work around issues with them, that post offers one of them.

Resources