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
Related
I am working on this problem for about a day now.
For some reason I am unable to TwoWay bind a value to a ComboBox if it is inside a ItemsControl. Outside works just fine.
I have an ObservableCollection of int? in my ViewModel:
private ObservableCollection<int?> _sorterExitsSettings = new ObservableCollection<int?>();
public ObservableCollection<int?> SorterExitsSettings
{
get { return _sorterExitsSettings; }
set
{
if (_sorterExitsSettings != value)
{
_sorterExitsSettings = value;
RaisePropertyChanged("SorterExitsSettings");
}
}
}
My XAML:
<ItemsControl ItemsSource="{Binding SorterExitsSettings}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.ScanRouter.Stores}"
SelectedValue="{Binding Path=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="name" SelectedValuePath="id" IsEditable="True" />
</DataTemplate>
</ItemsControl.ItemTemplate>
So the ComboBox is populated with a list of stores. It works fine so far.
The ObservableCollection SorterExitsSettings even has some values set which are shown in the displayed ComboBoxes. So setting the SelectedValue also works.
However when I change a selection, SorterExitsSettings wont change. While when I implement the ComboBoxes(100) without an ItemsControl it suddenly works fine.
<ComboBox ItemsSource="{Binding ScanRouter.Stores}" DisplayMemberPath="name" SelectedValuePath="id" IsEditable="True" SelectedValue="{Binding SorterExitsSettings[0], Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
Even better when I implement the ComboBoxes using the ItemsControl and also the example ComboBox shown above. When I change the single ComboBox's value it will change the value of the ComboBox inside the ItemsControl, but not the other way around.
Did somebody encounter this problem before?
My guess was that the ItemsControl doesn't like the fact that I am binding my selected value to an item in a list. However when I bind directly to a ViewModel property(Store) it also doesn't work.
I also tried using SelctedItem instead of SelectedValue and populate the ObservableCollection with Store objects instead of int?.
The problem is that you're binding your ComboBox's SelectedValue directly to the collection elements which are type int ?. This won't work, binding targets have to be properties. Try wrapping your int ? values in a class and expose the value as a property of that class with a getter and setter, i.e. something like this:
private ObservableCollection<Wrapper> _sorterExitsSettings = new ObservableCollection<Wrapper>();
... etc...
And:
public class Wrapper
{
public int? Value {get; set;}
}
And finally:
<ComboBox ... SelectedValue="{Binding Path=Value, Mode=TwoWay...
Post back here if you still have problems.
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?
Is it possible to bind one ObservableCollection to two Listbox ItemsSource's in the same view?
public ObservableCollection<CameraListBoxItem> Window1CameraListBoxItems
{
get { return cameraListBoxItems; }
}
<ListBox x:Name="DeviceList" ItemsSource="{Binding Window1CameraListBoxItems}" />
<ListBox x:Name="DeviceList2" ItemsSource="{Binding Window1CameraListBoxItems}" />
It doesn't seem to work and I don't understand why.
UPDATE
<DataTemplate DataType="{x:Type vm:WindowViewModel}">
<vw:WindowView />
</DataTemplate>
Using MVVM the view is binded to the viewmodel.
public WindowViewModel(ObservableCollection<CameraListBoxItem> items)
{
cameraListBoxItems = items;
}
Yes. You can bind as many elements as you wish to the same source property.
I would check the bindings debugging information in the Output Window. This may not be working because both bindings are incorrect, which could happen if the DataContext isn't set properly.
I am trying to find out how to set correctly multiple DataContexts in XAML page. I have a basic collection that I create in code behind and set ItemSource Binding og AutoCompleteBox to it. At the same time, I have another datacontext to set labelsDataSource inside the grid. If I set this datacontext, the AutoCompleteBox’s itemsSource binding is lost. AutoCompleteBox is inside that grid. I do assign DataContext directly to the objetc this way:
MyAutoCompleteBox.DataContext = this;
I am wondering if there is a better way to do it?
Thank you in advance for the help!
Setting AutoComplete Box:
<sdk:AutoCompleteBox x:Name="MyAutoCompleteBox" IsTextCompletionEnabled="True" ItemsSource="{Binding Items}" />
Code Behind:
public IList<string> Items
{
get;
private set;
}
public Basic_ChildWindow()
{
InitializeComponent();
Items = new List<string>();
Items.Add(#"One");
Items.Add(#"Two");
Items.Add(#"Three");
DataContext = this;
}
Another datacontext in the same XAML page, AutoCompleteBox is inside that grid:
<Grid x:Name="grdBasic_ChildWindow_Right" Style="{StaticResource GridStyle}" DataContext="{Binding Source={StaticResource LabelsDataSource}}">
I'm not sure I understand your question--what is "labelsDataSource"?
However, if what you have posted is all the code and there is nothing more to it, simply remove the datacontext/binding from the grid. The grid does not need a datacontext set (it is simply a visual container--not data-related).
So change this:
<Grid x:Name="grdBasic_ChildWindow_Right" Style="{StaticResource GridStyle}" DataContext="{Binding Source={StaticResource LabelsDataSource}}">
To this:
<Grid x:Name="grdBasic_ChildWindow_Right" Style="{StaticResource GridStyle}">
I've been going round in circles with this for a couple of days, and I'm hoping a WPF guru can see where I'm going wrong.
I'm setting CurrentViewModel in code. The Selected item of my ListBox and the Content of my ContentControl bind correctly. But when changing the selected item in the Listbox via the UI the CurrentViewModel is being set but the Content Control is not being updated.
I'm using a data template to map my Views and View Models.
<DataTemplate DataType="{x:Type ViewModel:MyViewModel}">
<View:MyView />
</DataTemplate>
I have a ListBox which is bound to an observable collection of ViewModels. The Selected Item is bound to the current view model.
<ListBox ItemsSource="{Binding MyViewModelCollection}" DisplayMemberPath="DisplayName" SelectedItem="{Binding CurrentViewModel, Mode=TwoWay}"/>
I also have a content control that is also bound to the CurrentView Model
<ContentControl Content="{Binding CurrentViewModel, Mode=TwoWay}"/>
This is the property that they are both bound to
public MyViewModel CurrentViewModel
{
get
{
return _currentViewModel;
}
set
{
if (_currentViewModel== value) return;
_currentViewModel= value;
OnPropertyChanged("CurrentViewModel");
}
}
I've edited the names for clarity and removed formatting information.
Any help greatly appreciated.
Cheers,
Daniel
EDIT: Came across the link How can I debug WPF bindings?. I set a break point on the Content binding and it does indeed only get called once when the binding is first set.
You should not be setting TwoWay as the mode on your ContentControl:
<ContentControl Content="{Binding CurrentViewModel, Mode=OneWay}"/>
This is because you intend your ContentControl to read the value, but never write it.
As an aside, you can also bind the ContentControl to the currently selected item in the collection, rather than to that property by doing this:
<ListBox ItemsSource="{Binding MyViewModelCollection}"
DisplayMemberPath="DisplayName"
IsSynchronizedWithCurrentItem="True"/>
<ContentControl Content="{Binding MyViewModelCollection/}"/>
The "slash" (/) at the end of the collection indicates the current item selected in the collection and setting that current item property is as simple as setting the IsSynchronizedWithCurrentItem equal to true.
A lot of times I find with this combination, I really don't need the extra property on my view model.
Anyway, I hope this helps.