WPF Cascading ComboBox to TextBox

Dec 23 2014 12:11 AM

'm new to Programming and have some difficulties with cascading ComboBox to TextBox. After few changes to ComboBox running this basic app Null error is generated. Please help. I'm trying to find anything about it but can't get any info.

MainWindow.xaml

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication26" x:Class="WpfApplication26.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Grid Margin="0,0,0,-1">
<ComboBox x:Name="Category" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" SelectionChanged="Category_SelectionChanged" Margin="309,52,0,0">
<ListBoxItem x:Name="clComboBoxItem1" Content="Comedy" Height="25"/>
<ListBoxItem x:Name="clComboBoxItem2" Content="Drama" Height="25"/>
<ListBoxItem x:Name="clComboBoxItem3" Content="Science Fisction" Height="25"/>
</ComboBox>
<ComboBox x:Name="Shows" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="309,79,0,0" SelectionChanged="Shows_SelectionChanged" SelectedItem="{Binding Name, ElementName=Category}"/>
<TextBox x:Name="TextBox1" HorizontalAlignment="Left" Height="23" Margin="309,106,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>
 
 
MainWindow.xaml.cs 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication26
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Category_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//Create List for Comedy selection
List<fruits> dropDownList_Comedy = new List<fruits>();
dropDownList_Comedy.Add(new fruits() { price = 10, Name = "Apples" });
dropDownList_Comedy.Add(new fruits() { price = 9, Name = "Bammamas" });
dropDownList_Comedy.Add(new fruits() { price = 1, Name = "Mango" });
//Create List for Drama selection
List<fruits> dropDownList_Drama = new List<fruits>();
dropDownList_Drama.Add(new fruits() { price = 10, Name = "Liver" });
//Create List for Science Fiction selection
List<fruits> dropDownList_SciFi = new List<fruits>();
dropDownList_SciFi.Add(new fruits() { price = 10, Name = "Apples2" });
//Check for SelectedIndex value and assign appropriate list to 2nd
if (Category.SelectedIndex == 0)
{
Shows.ItemsSource = dropDownList_Comedy;
Shows.DisplayMemberPath = "Name";
}
else if (Category.SelectedIndex == 1)
{
Shows.ItemsSource = dropDownList_Drama;
Shows.DisplayMemberPath = "Name";
}
else if (Category.SelectedIndex == 2)
{
Shows.ItemsSource = dropDownList_SciFi;
Shows.DisplayMemberPath = "Name";
}
}
private void Shows_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
{
// var Category = (ComboBox)sender;
//if (Category.SelectedItem == null) return ;
{
var Category = (ComboBox)sender;
if (Category.SelectedItem == null)
return;
}
{
fruits dropDownList_Comedy = Shows.SelectedItem as fruits;
TextBox1.Text = Convert.ToString(dropDownList_Comedy.price);
}
{
fruits dropDownList_Drama = Shows.SelectedItem as fruits;
TextBox1.Text = dropDownList_Drama.price.ToString();
}
{
fruits dropDownList_SciFi = Shows.SelectedItem as fruits;
TextBox1.Text = dropDownList_SciFi.price.ToString();
}
}
}
}
}
class1.cs
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApplication26
{
class fruits
{
public int price { get; set; }
public string Name { get; set; }
}
}