displaying ComboBox.SelectedValue in TextBox - wpf

I am working on a WPF and have hit a serious wall. I have a data set that has two columns, ContactName and ContactTitle. I have successfully loaded all of the data into a ComboBox and even sorted it by ContactName. However, I am trying to now access that data and display part of it in a TextBox. (This is of course just a proof of concept type exercise, the final product will populate a variety of TextBoxes with the selected persons information). The problem is, I cannot get the info to populate in the TextBox. Here is the code that I have:
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
namespace MultiBindingInWPF_CS
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
//Create DataSet
CustomersDataSet customerDataSet = new CustomersDataSet();
//Create DataTableAdapter
CustomersDataSetTableAdapters.CustomersTableAdapter taCustomers = new CustomersDataSetTableAdapters.CustomersTableAdapter();
taCustomers.Fill(customerDataSet.Customers);
//Sort Data
SortDescription sd = new SortDescription("ContactName", ListSortDirection.Descending);
//Designate ItemSource
this.ComboBox1.ItemsSource = customerDataSet.Customers;
//Apply Sort
this.ComboBox1.Items.SortDescriptions.Add(sd);
}
private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
//Using SelectedIndex only to prove connection to TextBox is working
textBox1.Text = ComboBox1.SelectedIndex.ToString();
}
catch
{
textBox1.Text = "Invalid";
}
}
}
}
Then here is my XAML:
<Window x:Class="MultiBindingInWPF_CS.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
Title="Multibinding in WPF" Height="163" Width="300">
<Grid Loaded="Grid_Loaded">
<StackPanel Name="StackPanel1" Margin="12">
<Label Height="28" Name="Label1">List of Customers (Name AND Title :-) )</Label>
<ComboBox Height="23" Name="ComboBox1" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" IsTextSearchEnabled="True" SelectionChanged="ComboBox1_SelectionChanged" SelectedValue="{Binding Path=CustomerID}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock DataContext="{Binding}">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding Path="ContactName" />
<Binding Path="ContactTitle" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBox Height="23" Name="textBox1" Width="120"/>
</StackPanel>
</Grid>
</Window>
My ultimate goal would be to populate the TextBox dynamically by getting the selected value, and getting the info in the dataset associated with that CustomerID, but just getting the SelectedItem's text to populate in the TextBox would be a huge step.
Any help is GREATLY appreciated. Thanks all.

Give this a try; it removes the changed event handler and leverages binding.
<Grid Loaded="Grid_Loaded">
<StackPanel Name="StackPanel1" Margin="12">
<Label Height="28" Name="Label1">List of Customers (Name AND Title :-) )</Label>
<ComboBox Height="23" Name="ComboBox1" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" IsTextSearchEnabled="True" SelectedValue="{Binding Path=CustomerID}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock DataContext="{Binding}">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding Path="ContactName" />
<Binding Path="ContactTitle" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBox Height="23" Name="textBox1" Text="{Binding ElementName=ComboBox1, Path=SelectedItem.ContactName}" Width="120"/>
</StackPanel>
</Grid>
Check out this SO answer as well, which details the differences between SelectedItem, SelectedValue, and SelectedValuePath and is ultimately the issue most people run into.

Related

Bind Multibinding Textbox in WPF MVVM

I have 3 TextBoxes bind with my class(Transaction) properties like this
<TextBox Text="{Binding Path=Transaction.Bills100,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Name="bills100" Grid.Column="2" Grid.Row="1" Margin="7"></TextBox>
<TextBox Text="{Binding Path=Transaction.Bill50,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Name="bills50" Grid.Column="2" Grid.Row="2" Margin="7"></TextBox>
<TextBox Text="{Binding Path=Transaction.Bill20,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Name="bills20" Grid.Column="2" Grid.Row="3" Margin="7"></TextBox>
Also I have another TextBox where I have done multibinding and done addition of the first three Textboxes like
<TextBox Grid.Column="2" IsReadOnly="True" Grid.Row="7" Grid.ColumnSpan="2" Margin="7" Name="TotalBills">
<TextBox.Text>
<MultiBinding Converter="{ikriv:MathConverter}" ConverterParameter="x+y+z" Mode="TwoWay">
<Binding Path="Text" ElementName="bills100" />
<Binding Path="Text" ElementName="bills50" />
<Binding Path="Text" ElementName="bills20" />
</MultiBinding>
</TextBox.Text>
</TextBox>
I want to bind this multibinding textbox with my class(Transaction) with property as Transaction.Total like my first three textboxes but it shows error
Property text is set more than once
Actually we cannot get the value of a two-way binding from one property and then set the value of another property.
Finally I came with a solution like this
In my Class Transaction
private double _totalBills;
public double TotalBills
{
get { return _totalBills; }
set { _totalBills= value; Notify("TotalBills"); }
}
In XAML(Instead of Multibinding)
<TextBox Text="{Binding Path=Transaction.TotalBills,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" IsReadOnly="True" Grid.Row="7" Grid.ColumnSpan="2" Margin="7" Name="TotalBills"/>
My ViewModel
public class MainViewModel: INotifyPropertyChanged
{
private Transaction _transactionDetails;
public MainViewModel()
{
Transaction= new Transaction();
_transactionDetails.PropertyChanged += _transactionDetails_PropertyChanged;
}
private void _transactionDetails_PropertyChanged(object sender,PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "TotalBills":
_calculate(); //My method for calculation
break;
}
}
}

How to copy Items from Datagrid to Listbox

I have a listbox, datagrid and a Button. The data grid is populated with data from MS SQL. I want to be able to copy selected item(s) from the datagrid to listbox using the button. The code behind the button is
private void btnAdd_click(object sender, RoutedEventArgs e)
{
lstSelected.Items.Add(iFacilitiesDataGrid.SelectedItem.ToString());
}
//List Box in xaml
<ListBox Grid.Column="2" Grid.Row="1" Grid.RowSpan="7" Height="258" HorizontalAlignment="Left" Margin="0,4,0,0" Name="lstSelected" VerticalAlignment="Top" Width="236" />
For the datagrid
<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Grid.Column="2" Grid.Row="1" Grid.RowSpan="7" Height="244" HorizontalAlignment="Left" ItemsSource="{Binding Source={StaticResource iLocationICategoriesIFacilitiesViewSource}}" Margin="291,5,0,0" Name="iFacilitiesDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="247">
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="facilityNameColumn" Header="Facility Name" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=FacilityName}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="priceColumn" Binding="{Binding Path=Price}" Header="Price" Width="100" />
</DataGrid.Columns>
</DataGrid>
Whenever I try to add item on the listbox, the item passed to the listbox is "HM.IFacility" Where HM is the project name and IFacility the Table Name .
If you want the instance of the object to enter the listbox, you cannot control the tostring method like you do:
lstSelected.Items.Add(iFacilitiesDataGrid.SelectedItem.ToString());
Instead, cast your selecteditem to the type:
lstSelected.Items.Add((HM.IFacility)iFacilitiesDataGrid.SelectedItem);
If you don't want the object to enter the listbox, but just some properties from that object, then you have to override the tostring in your object. If you don't do that, the tostring will give you the type of the object, like you said --> "HM.IFacility"
to override the tostring, put this in your object class:
public override string ToString()
{
return property1 + " " + property2
}
Where property1 and property2 are properties on your object class (like name, age, or id)
You need to create a datatemplate for listbox as well. as you have created for DataGrid. replace your listbox with below one and try again.
<ListBox Grid.Column="2" Grid.Row="1" Grid.RowSpan="7" Height="258" HorizontalAlignment="Left" Margin="0,4,0,0" Name="lstSelected" VerticalAlignment="Top" Width="236" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=FacilityName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Condition: The data of DataGrid and ListBox must be same Type.

One combo box filtering another combo box in a WPF Datagrid

I have a datagrid which has two combo box columns in it. The first combo box is a list of PersonnelTypes. Depending on which PersonnelType is selected, the second combo box should fill up with a list of Resources that match the selected PersonnelType
The problem is, lets say I have two rows of data, if I change the PersonnelType of one row, the datagrid will set the itemsource for all of the Resources in every row. I only want it to filter the row that I am in, not all the rows.
Here's the xaml for the part of the datagrid that has the combo boxes:
<DataGridTemplateColumn Header="Personnel Type" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<ComboBox Name="cmbPersonnelTypes" FontWeight="Bold" ItemsSource="{Binding ViewModel.PersonnelTypes, RelativeSource={RelativeSource AncestorType=Window}}" SelectedItem="{Binding PersonnelType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="ID" DisplayMemberPath="Description" SelectionChanged="cmbPersonnelTypes_SelectionChanged" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Name" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<ComboBox Name="cmbPersonnelName" FontWeight="Bold" ItemsSource="{Binding ViewModel.ResourcesToChooseFrom, RelativeSource={RelativeSource AncestorType=Window},UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Resource, Mode=TwoWay}" SelectedValuePath="Refno" DisplayMemberPath="Name" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Here is the xaml for the whole data grid (just in case you need to see it):
<DataGrid AutoGenerateColumns="False" CanUserSortColumns="False" CanUserDeleteRows="True" IsReadOnly="True" Background="LightGray" CanUserAddRows="False" Margin="5" SelectedItem="{Binding SelectedLA_JobPersonnel}" ItemsSource="{Binding LA_Personnel}" Grid.ColumnSpan="4" MouseDoubleClick="DataGrid_MouseDoubleClick_1">
<DataGrid.Resources>
<ViewModels:BindingProxy x:Key="proxy" Data="{Binding}" />
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Personnel Type" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<ComboBox Name="cmbPersonnelTypes" FontWeight="Bold" ItemsSource="{Binding ViewModel.PersonnelTypes, RelativeSource={RelativeSource AncestorType=Window}}" SelectedItem="{Binding PersonnelType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="ID" DisplayMemberPath="Description" SelectionChanged="cmbPersonnelTypes_SelectionChanged" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Name" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<ComboBox Name="cmbPersonnelName" FontWeight="Bold" ItemsSource="{Binding ViewModel.ResourcesToChooseFrom, RelativeSource={RelativeSource AncestorType=Window},UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Resource, Mode=TwoWay}" SelectedValuePath="Refno" DisplayMemberPath="Name" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn> <DataGridTemplateColumn Header="Date Out" Width="20*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Background="LightGray" FontWeight="Bold" Text="{Binding DateOut, Mode=TwoWay, Converter={StaticResource thisNullDateConverter}, StringFormat={}{0:MMM-dd-yyyy hh:ss tt}}">
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<Toolkit:DateTimePicker Background="LightGray" FontWeight="Bold" Value="{Binding Path=DateOut, Mode=TwoWay, Converter={StaticResource thisNullDateConverter}}" Format="Custom" FormatString="MMM dd yyyy hh:ss tt"></Toolkit:DateTimePicker>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Date In" Width="20*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Background="LightGray" FontWeight="Bold" Text="{Binding DateIn, Mode=TwoWay, Converter={StaticResource thisNullDateConverter}, StringFormat={}{0:MMM-dd-yyyy hh:ss tt}}">
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<Toolkit:DateTimePicker Background="LightGray" FontWeight="Bold" Value="{Binding Path=DateIn, Mode=TwoWay, Converter={StaticResource thisNullDateConverter}}" Format="Custom" FormatString="MMM dd yyyy hh:ss tt"></Toolkit:DateTimePicker>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Here is the code behind for the xaml (xaml.cs):
public JobEditorViewModel ViewModel
{
get { return viewModel; }
}
private void cmbPersonnelTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var combobox = sender as ComboBox;
if (combobox != null)
{
var selectedPersonnelType = combobox.SelectedItem as PersonnelType;
viewModel.SetResourcesToChooseFrom(selectedPersonnelType);
}
}
Here is the code in the viewModel:
public BindingList<PersonnelType> PersonnelTypes
{
get; set;
}
public JobEditorViewModel(int jobid, string region, DataAccessDataContext db, ServiceUserControlViewModel serviceViewModel)
{
PersonnelTypes = new BindingList<PersonnelType>(_db.PersonnelTypes.OrderBy(p => p.Head).ThenBy(p => p.Description).ToList());
}
private BindingList<Resource> _resourcesToChooseFrom;
public BindingList<Resource> ResourcesToChooseFrom
{
get { return _resourcesToChooseFrom; }
set
{
_resourcesToChooseFrom = value;
NotifyPropertyChanged("ResourcesToChooseFrom");
}
}
public void SetResourcesToChooseFrom(PersonnelType personnelType)
{
ResourcesToChooseFrom =
new BindingList<Resource>(_db.Resources.Where(r => r.Head == personnelType.Head && r.Refno > 2).OrderBy(r=>r.Name).ToList());
}
If you need to see more, let me know
Well, with some help from a colleague here at work, we figured out what I needed to do. Multibinding is the answer. First off we kind of hacked around so that the two combo boxes could be in the same column by placing them both in a grid and placing the grid in the one column. So now both combo boxes can see each other because they are in the same DataGridTemplateColumn. Before, we couldn't get them to see each other because they lost scope of each other in being two separate DataGridTemplateColumns.
Here's what we did in the xaml:
<DataGridTemplateColumn Header="Personnel Type-Name" Width="Auto" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="170"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" BorderBrush="Black" BorderThickness="1">
<TextBlock Text="{Binding PersonnelType.Description}"/>
</Border>
<Border Grid.Column="1" BorderBrush="Black" BorderThickness="1">
<TextBlock Text="{Binding Resource.Name}"/>
</Border>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="170"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ComboBox Name="cmbPersonnelTypes" Grid.Column="0" FontWeight="Bold" ItemsSource="{Binding ViewModel.PersonnelTypes, RelativeSource={RelativeSource AncestorType=Window}, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding PersonnelType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="ID" DisplayMemberPath="Description" />
<ComboBox Name="cmbPersonnelName" Grid.Column="1" FontWeight="Bold" SelectedItem="{Binding Resource, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="Refno" DisplayMemberPath="Name" >
<ComboBox.ItemsSource>
<MultiBinding Converter="{StaticResource FilteredPersonnelConverter}">
<Binding Path="ViewModel.AvailablePersonnel" RelativeSource="{RelativeSource AncestorType=Window}"/>
<Binding Path="SelectedItem" ElementName="cmbPersonnelTypes"/>
<Binding Path="ViewModel.SelectedGlobalResourceViewOption" RelativeSource="{RelativeSource AncestorType=Window}"/>
</MultiBinding>
</ComboBox.ItemsSource>
</ComboBox>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
You'll notice there is a ValueConverter in the MultiBinding called FilteredPersonnelConverter. This value converter does all the filtering for me. Here's the code for that:
public class FilteredPersonnelListValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var allResources = values[0] as IList<Resource>;
var personnelType = values[1] as PersonnelType;
var selectedGlobalResourceView = values[2] as ResourceViewOption;
if (personnelType == null)
return allResources;
if(selectedGlobalResourceView.ResourceViewTitle=="Regional")
return allResources.Where(r => r.Head == personnelType.Head && r.Obsolete == false && r.Location.Region.RegionID.Trim()==SettingsManager.OpsMgrSettings.Region.Trim()).OrderBy(r => r.Name).ToList();
if (selectedGlobalResourceView.ResourceViewTitle == "Local")
return allResources.Where(r => r.Head == personnelType.Head && r.Obsolete == false && r.LocnID.Trim() == SettingsManager.OpsMgrSettings.LOCNCODE.Trim()).OrderBy(r => r.Name).ToList();
return allResources.Where(r => r.Head == personnelType.Head &&r.Obsolete==false).OrderBy(r => r.Name).ToList();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
So if anyone else is doing something like this, look into Multibinding, it will change your life
When the user changes the PersonelType dropdown in the view, the ViewModel should then filter the list of Resources (which should update the second dropdown box with the correct information).
Really your view model just needs to be set up to respond to changes on the view. That's what it boils down to.
Looking here:
public JobEditorViewModel(int jobid, string region, DataAccessDataContext db, ServiceUserControlViewModel serviceViewModel)
{
PersonnelTypes = new BindingList<PersonnelType>(_db.PersonnelTypes.OrderBy(p => p.Head).ThenBy(p => p.Description).ToList());
}
it looks like you set the personel types in the constructor, but you aren't responding to user changes. You need to do that in the PersonelType Binding in your view model.
EDIT
I see now that you're handling the selection changed. But really I think this should be done in the view model itself. Specifically because you actually access the viewmodel from the view to do make your changes.
Example
So here's my VM:
class ViewModel : INotifyPropertyChanged
{
DispatcherTimer timer = new DispatcherTimer();
public ViewModel()
{
// Create my observable collection
this.DateTimes = new ObservableCollection<DateTime>();
// Every second add anothe ritem
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
// This adds to the collection
this.DateTimes.Add(DateTime.Now);
}
public ObservableCollection<DateTime> DateTimes { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
}
and My View:
<ListBox ItemsSource="{Binding DateTimes}"/>
Notice that I don't rebuild the collection. I just set it once, and then change it's size as neccesary.

WPF combobox dynamic binding

I've a combo box with in data grid edititemtemplate and i write some code in combo box loaded event like:
Code:
private void cmbGFld_Loaded(object sender, RoutedEventArgs e)
{
ComboBox cmb = (ComboBox)sender;
cmb.ItemsSource = FieldsList.GetFieldList();
ConditionField cData = condLists[FieldGrid.SelectedIndex];
cmb.SelectedItem = cData.FieldType;
}
XAML Code:
<toolkit:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate x:Name="editTemplate">
<ComboBox Loaded="cmbGFld_Loaded" BorderBrush="Transparent" SelectedItem="{Binding Path=FieldType}" SelectedValuePath="Name" BorderThickness="0" FontSize="13" FontStyle="Italic" FontWeight="Normal" Foreground="DimGray" x:Name="cmbGFld" Template="{StaticResource ComboBoxTemplate2}">
<ComboBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">Green</SolidColorBrush>
</ComboBox.Resources>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Left" Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</toolkit:DataGridTemplateColumn.CellEditingTemplate>
But the problem is when ever i try to edit the combo box it doesn't showing which is already selected, any one help me.
Thanks,
#nag.
Try it without cmb.SelectedItem = cData.FieldType; in cmbGFld_Loaded(). This will overwrite the binding SelectedItem="{Binding Path=FieldType}" in your XAML. Set the selected item in the binded FieldType property instead. I don't know your application but something like:
FieldType = condLists[FieldGrid.SelectedIndex].FieldType;

TreeViewItem with Databinding, Childs doesn't update

I've got a Treeview with a hierachial template.
Everything works fine. All Objets will respond as expected.
But adding elements to the collection doesn't update the treeview.
My base Object is bind to the treeview.
One of its propertys contains a collection. And this collection has got a property with an own collection.
BaseObject
-> Sub Collection 1
-> SubCollection 2
My BaseObject has implemented INotifyPropertyChanged and my SubCollection 2 has implemented ICollectionChaged.
Nevertheless, wehen I try to add a new Item to SubCollection 2 OnCollectionChaged is called, but CollectionChanged stays null, so nothing happens.
TreeView Templates:
<HierarchicalDataTemplate x:Key="SheetTreeTemplate" >
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Margin="3,0" Source="/Resources/Icons/page_green.png" />
<TextBlock FontStyle="Italic">
<TextBlock.Text>
<MultiBinding StringFormat="{}Seite {0}">
<Binding Path="Name"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="DocumentTreeTemplate" ItemsSource="{Binding Path=Sheets.Values}" ItemTemplate="{StaticResource SheetTreeTemplate}">
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Margin="3,0" Source="/Resources/Icons/folder.png" />
<TextBlock FontStyle="Italic">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="DocTypName"/>
<Binding Path="ID"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="PileTreeTemplate" ItemsSource="{Binding Path=Documents.Values}" ItemTemplate="{StaticResource DocumentTreeTemplate}">
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Margin="3,0" Source="/Resources/Icons/report.png" />
<TextBlock FontStyle="Italic" Text="{Binding Path=Name}" />
</StackPanel>
</HierarchicalDataTemplate>
TreeView itself:
<TreeView Style="{DynamicResource NavigationTree}" Name="tvw_mainMenu" ItemsSource="{Binding Values}" ItemTemplate="{DynamicResource PileTreeTemplate}" SelectedItemChanged="tvw_mainMenu_SelectedItemChanged"/>
the Class which should subscribe the SubCollection 2 Changed:
class Sheets : Dictionary<String, Sheet> , INotifyCollectionChanged {
public bool Add(String sKey, Sheet newSheet) {
base.Add(sKey, newSheet);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, new KeyValuePair<String, Sheet>(sKey, newSheet)));
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e) {
if (CollectionChanged != null) {
CollectionChanged(this, e);
}
}
}
i've found out that there are much more interfaces to implement. Best way is an observable Collection.
Cause I don't want to change all my classes i've found ObservableDictionary
example.

Resources