Can't bind ObservableCollection to Combobox in wpf - c#

I have the following xaml:
<UserControl.Resources>
<sivm:Locator x:Key="viewModelLocator" ModelType="{x:Type ViewModels:RateVSConcentrationViewModel}"/>
</UserControl.Resources>
<UserControl.DataContext>
<Binding Path="ViewModel" Source="{StaticResource viewModelLocator}"/>
</UserControl.DataContext>
...
<ComboBox Grid.Column="0" Grid.Row="1" Height="20" VerticalAlignment="Top" ItemsSource="{Binding Chambers}" > <!--SelectedItem="{Binding SelectedChamber}">-->
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ChamberName}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Chambers is an ObservableCollection of objects that contains a property called ChamberName which I want displayed in the combobox.
In the ViewModel I have the following code:
foreach (var chamber in chambers)
{
Chambers.Add(chamber);
}
OnPropertyChanged(() => Chambers);
But when this code executes the combobox is not updated. I have used this way to do a lot of databinding but I can't get this to work.
Can someone see what I am doing wrong here?

You may need to set the relativesource on your textblock, try modifying the binding on the Text property of your textblock to the following:
{Binding DataContext.ChamberName, RelativeSource={RelativeSource AncestorType=ComboBox}}
And as nit said above, always check for binding errors, they will put you on the right path.

You should adjust the property binding as follows:
<ComboBox ItemsSource="{Binding Path=ViewModel.Chambers, Source={StaticResource viewModelLocator}}" >

I got it to work doing this:
<ComboBox DisplayMemberPath="ChamberName" Grid.Column="0" Grid.Row="1" Height="20" VerticalAlignment="Top" ItemsSource="{Binding Chambers}"> <!--SelectedItem="{Binding SelectedChamber}">-->

Related

WPF, Caliburn.micro : How to bind property outside of current binding context?

<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Key}"></TextBlock>
<ComboBox ItemsSource="{Binding Value}" SelectedItem="{Binging SomeProperty}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
I am inside a Dictionary , I want to bind SelectedItem="{Binging SomeProperty}" SomeProperty is a property of the ViweModel, not inside of the Dictionary. How to do that? How can I bind to properties outside of current binding context.
In case of Binding to the Window.DataContext's property, you may do it the following way with RelativeSource:
<ComboBox ItemsSource="{Binding Value}" SelectedItem="{Binding DataContext.SomeProperty, RelativeSource={RelativeSource AncestorType=Window}}">
Or in case you want to display SelectedItem for example in some TextBox (for example collection of string):
<TextBox Text="{Binding MyCollection/}"/>
<ComboBox ItemsSource="{Binding MyCollection}" IsSynchronizedWithCurrentItem="True">
MyCollection with "/" gets the current item of the ICollectionView used as DefaultView for MyCollection. Read more >>>

How to bind properties of one ObservableCollection of ListView to properties of SelectedItem of another ListView?

So I have a few ListViews. The first is binded to ObservaleCollection<ComPort>. All properties of ComPort may take some predefined values. Other ListViews are responsible for that properties: they show all that possible (predefined) values and SelectedItem should be the current value of that property of ComPort from the first ObservaleCollection.
I can't attach images so here is an external picture, it would make the situation clean: http://i.stack.imgur.com/ZBRRx.png
<Window.Resources>
<ResourceDictionary x:Name="rd">
<l:ComPorts x:Key="vComPorts"/>
<l:SystemPorts x:Key="vSystemPorts"/>
<l:BaudRates x:Key="vBaudRate"/>
<l:Parities x:Key="vParities"/>
<l:DataBits x:Key="vDataBits"/>
<l:StopBits x:Key="vStopBits"/>
<l:Timeouts x:Key="vTimeouts"/>
<l:ComPort x:Key="vSelectedPort"/>
</ResourceDictionary>
</Window.Resources>
...
<ListView
Name="PortsList"
Grid.Row="1"
Grid.Column="0"
Margin="5"
VerticalAlignment="Stretch"
ItemsSource="{StaticResource vComPorts}"
DataContext="{StaticResource vComPorts}"
SelectedValuePath="PortName"
SelectedValue="{Binding ElementName=SystemPortsList, Path=SelectedItem.Value}"
SelectionChanged="PortsList_SelectionChanged"
MouseDoubleClick="PortsList_MouseDoubleClick">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox />
<TextBlock Margin="5,0,0,0" Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView
x:Name="SystemPortsList"
Margin="5"
VerticalAlignment="Stretch"
DataContext="{Binding Source={StaticResource vSelectedPort}}"
ItemsSource="{Binding Source={StaticResource vSystemPortsView}}"
SelectedItem="{Binding Source={StaticResource vSelectedPort}, Path=PortName}"
MouseEnter="SystemPortsList_Refresh"
MouseLeave="SystemPortsList_Refresh"
Grid.Row="1"
Grid.Column="1" SelectionChanged="SystemPortsList_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Name="tb" Margin="5,0,0,0" Text="{Binding Path=Name}" />
</StackPanel>
</ListView.ItemTemplate>
</ListView>
I've tried to make an instance of class ComPort for saving current value of selected item from the first ListView, but anyway I can't cope with it without help. How this task should be solved?
1) Instead of handling SelectionChanged on the PortsList ListView, bind your checkbox to the ListViewItemsPanel like so:
<CheckBox IsChecked={Binding IsSelected, RelativeSource=Parent/>
2) Add an x:Name to your first ListBox, say x:Name="ComPortLB";
3) Remove DataContext on SystemPortsList;
4) Fix SelectedItem on SystemPortsList like so:
SelectedValue="{Binding ElementName=ComPortLB, Path=SelectedValue.PortName}"
I haven't tested any of this code and I haven't done this kind of stuff for a while, so I apologize for errors, but it should get you closer. I've also had to make some assumptions about your classes since you don't provide enough information.

How can i bind a button in listviewitem to a Command in ViewModel in Winrt

I have a NavigateToAccountsCommand RelayCommand property in the ViewModel. When I bind the same to a button on the page anywhere outside the ListView the command binding is working. However as soon as I move this to a ListView's DataTemplate its not working.
I have tried changing the binding from NavigateToAccountsCommand to DataContext.NavigateToAccountsCommand still not working.
Appreciate your help...
<Page
x:Class="FinancePRO.App.Views.AccountsView"
DataContext="{Binding AccountsViewModel, Source={StaticResource MainViewModelLocator}}"
mc:Ignorable="d">
<Grid>
<!--**This one is working**-->
<Button Command="{Binding NavigateToAccountsCommand}" >
<!--**This one is not working**-->
<ListView ItemsSource="{Binding AllAccounts}" >
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Stretch">
<TextBlock Text="{Binding AccountName}"/>
<Button Command="{Binding NavigateToAccountsCommand}">
</Button>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
When you are inside the DataTemplate of the ListView, your data context is the current item of the ListView's ItemsSource. As there is nothing called "NavigateToAccountsCommand" property within your AllAcounts' each individual element, binding isn't working.
To fix that, you will need to reference something from the outside of DataTemplate; following should work. It changes the binding to reference the root Grid's DataContext which should have the property NavigateToAccountsCommand accessible. To reference the grid, you have to add Name attribute, and then use ElementName binding.
<Grid Name="Root">
<!--**This one is working**-->
<Button Command="{Binding NavigateToAccountsCommand}" >
<!--**This one is not working**-->
<ListView ItemsSource="{Binding AllAccounts}" >
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Stretch">
<TextBlock Text="{Binding AccountName}"/>
<Button Command"{Binding ElementName=Root, Path=DataContext.NavigateToAccountsCommand}">
</Button>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
You can use
<Button x:Name="cmdTabItemCloseButton"
Style="{StaticResource TabItemCloseButtonStyle}"
Grid.Column="1" Margin="15,0,0,0"
Command="{Binding RelativeSource=
{RelativeSource FindAncestor,
AncestorType={x:Type ListView}},
Path=DataContext.NavigateToAccountsCommand}"
CommandParameter="{Binding}"/>
I had a similar problem (Win RT) which I solved by just using:
<GridView
x:Name="itemGridView"
ItemClick="ItemView_ItemClick"
IsItemClickEnabled="True"/>
and then in the Page class:
private void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
//e is the object being clicked
}

How can I bind the SelectedItemValue from a TreeView back to a ViewModel in Silverlight?

I have a TreeView of items, and a corresponding TreeViewModel. I want to support a property on the TreeViewModel which keeps a track of the currently selected item in the TreeView.
This is the code I have:
<UserControl.DataContext>
<vm:TreeMenuViewModel />
</UserControl.DataContext>
<UserControl.Resources>
<common:HierarchicalDataTemplate x:Key="MenuItemsTemplate"
ItemsSource="{Binding ChildItems}">
<TextBlock Text="{Binding MenuOption}"/>
</common:HierarchicalDataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<toolkit:DockPanel>
<sdk:TreeView
Name="treeView1"
VerticalAlignment="Stretch"
toolkit:DockPanel.Dock="Left"
Background="Transparent"
ItemsSource="{Binding ChildItems}"
ItemTemplate="{StaticResource MenuItemsTemplate}"
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedItemChanged">
<ei:ChangePropertyAction
TargetObject="{Binding}"
PropertyName="SelectedItemId"
Value="{Binding MenuOptionId}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</sdk:TreeView>
</toolkit:DockPanel>
</Grid>
The TreeView binds and displays the data correctly. The trigger is also working and targeting the correct property - when I break on the SelectedItemId property in the TreeViewModel, the property is being hit, but the value supplied is always 0. How can I supply the value of the selected item?
Just remove the whole trigger and add:
ItemsSource="{Binding ChildItems}"
SelectedItem="{Binding SelectedChild}"
And then your ViewModel needs a SelectedChild property. Your VM can react to changes from the setter of that property.
If you want 2-way binding then make it a NotifyChange property.
And I think you want the id: SelectedChild.Id or something like that.
Try using the TreeView SelectedValue and SelectedValuePath properties:-
<Grid x:Name="LayoutRoot" Background="White">
<toolkit:DockPanel>
<sdk:TreeView
Name="treeView1"
VerticalAlignment="Stretch"
toolkit:DockPanel.Dock="Left"
Background="Transparent"
ItemsSource="{Binding ChildItems}"
ItemTemplate="{StaticResource MenuItemsTemplate}"
SelectedValue="{Binding SelectedItemId, Mode=TwoWay}"
SelectedValuePath="MenuOptionId"
/>
</toolkit:DockPanel>
</Grid>
why not use something like this.
(SingleObjectInYourList)Item = treeview.SelectedItem;

How to synchronize a TextBox with a ListBox in WPF using IsSynchronizedWithCurrentItem

I'm starting with WPF and I have this pretty easy question:
I have a TextBlock and a Listbox that share the same DataContext. The ItemsSource of the ListBox is set to point to a property of the DataContext that its an ObservableCollection. I want that the TextBlock to contain the selected item of the Listbox. Some code:
View view = new View();
view.DataContext = new ViewModel();
view.Show();
<TextBlock Name="textBox1" Grid.Row="0" Grid.Column="0" Margin="1" Text="{Binding ¿xxx?}"></TextBlock>
<ListBox Name="listBox1" Grid.Row="1" Grid.ColumnSpan="2" Margin="1" ItemsSource="{Binding Model.BinariesToDeploy}" IsSynchronizedWithCurrentItem="True" />
Hope its clear.
If you actually want to use the synchronization you need to bind to the current item of the collection which will be set by the ListBox or any other control which has IsSynchronizedWithCurrentItem set to true, to do so use the /:
<TextBlock Text="{Binding Model.BinariesToDeploy/}" />
When the source is a collection view, the current item can be specified with a slash (/). For example, the clause Path=/ sets the binding to the current item in the view. When the source is a collection, this syntax specifies the current item of the default collection view.
The current item is managed by the CollectionView which is a layer on top of your original collection, CollectionViews can also be used for filtering, sorting and grouping.
An example:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<x:Array x:Key="items" Type="{x:Type Label}">
<Label Content="Apple" Tag="Fruit"/>
<Label Content="Pear" Tag="Fruit"/>
<Label Content="Orange" Tag="Fruit"/>
<Label Content="Lime" Tag="Fruit"/>
<Label Content="Tomato" Tag="Vegetable"/>
<Label Content="Radish" Tag="Vegetable"/>
<Label Content="Lettuce" Tag="Vegetable"/>
</x:Array>
</Page.Resources>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<StackPanel>
<ListBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource items}}"/>
<!-- Binds to CurrentItem.Content -->
<ContentControl Content="{Binding /Content,Source={StaticResource items}}"/>
</StackPanel>
</ScrollViewer>
</Page>
try something like this
Text = "{Binding ElementName=listBox1, Path=SelectedValue.Content}"

Categories