Reputation: 75
What I have are 4 Combo Boxes, each labelled within the dropdown menu. After that, within my code I want to setup an if else statement for the ComboBox. if the item is selected, it gets added into a DataGrid based on certain values (Cost, item Type, and name of the Item).
XAML of the ComboBox (Shortened for this question)
<TextBlock>Electronics:</TextBlock>
<ComboBox>
<ComboBoxItem x:Name="cmbxElectronics">
<TextBlock x:Name="txtLaptop">Laptop</TextBlock>
</ComboBoxItem>
<ComboBoxItem>
<TextBlock x:Name="txtPC">PC</TextBlock>
</ComboBoxItem>
<ComboBoxItem>
<TextBlock x:Name="txtStove">Stove</TextBlock>
</ComboBoxItem>
<ComboBoxItem>
<TextBlock x:Name="txtCamera">Camera</TextBlock>
</ComboBoxItem>
</ComboBox>
Here is the Data Grid XAML Code:
<StackPanel Grid.Row="0" Grid.Column="1" Margin="5">
<DataGrid Margin="5" Height="195">
<DataGrid.Columns>
<DataGridTextColumn Header=" Item" Width="*" />
<DataGridTextColumn Header=" Type" Width="*"/>
<DataGridTextColumn Header=" Cost($)" Width="*"/>
</DataGrid.Columns>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="#3280fc"/>
<Setter Property="FontWeight" Value="SemiBold"/>
<Setter Property="BorderThickness" Value="0,0,1,2"/>
<Setter Property="BorderBrush" Value="Black"/>
</Style>
</DataGrid.Resources>
</DataGrid>
</StackPanel>
Lastly, here is the code in the cs file which creates the class for the DataGrid (Invoice) and the Method to calculate totals (Just want to know how to set the ComboBox to the DataGrid):
private class Invoice
{
public string Item { get; set; }
public string Type { get; set; }
public double Price { get; set; }
}
private void Calculate()
{
double price = 0;
const double HST = 0.18;
if (cmbxElectronics.IsSelected)
{
}
}
Once I figure it out for one of these, the rest should be straightforward. Let me know if more information is required, Thank you.
Upvotes: 0
Views: 1099
Reputation: 3670
Mainly pay attention to binding:
<DataGrid x:Name="DataGridInvoices" ItemsSource="{Binding}" Height="236" Width="636" AutoGenerateColumns="False" CanUserAddRows="False" >
<DataGrid.Columns >
<DataGridTextColumn Header=" Item" Width="*" Binding="{Binding Item}"/>
<DataGridTextColumn Header=" Type" Width="*" Binding="{Binding Type}"/>
<DataGridTextColumn Header=" Cost($)" Width="*" Binding="{Binding Price}"/>
</DataGrid.Columns>
Remove duplicate columns and remove last blank row:
AutoGenerateColumns="False" CanUserAddRows="False"
Xaml:
<Window x:Class="WpfApp5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp5"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBlock>Electronics:</TextBlock>
<StackPanel Grid.Row="0" Grid.Column="1" Margin="5">
<DataGrid x:Name="DataGridInvoices" ItemsSource="{Binding}" Height="236" Width="636" AutoGenerateColumns="False" CanUserAddRows="False" >
<DataGrid.Columns >
<DataGridTextColumn Header=" Item" Width="*" Binding="{Binding Item}"/>
<DataGridTextColumn Header=" Type" Width="*" Binding="{Binding Type}"/>
<DataGridTextColumn Header=" Cost($)" Width="*" Binding="{Binding Price}"/>
</DataGrid.Columns>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="#3280fc"/>
<Setter Property="FontWeight" Value="SemiBold"/>
<Setter Property="BorderThickness" Value="0,0,1,2"/>
<Setter Property="BorderBrush" Value="Black"/>
</Style>
</DataGrid.Resources>
</DataGrid>
</StackPanel>
<ComboBox x:Name="ComboBox1" HorizontalAlignment="Left" Margin="298,300,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="ComboBox1_SelectionChanged">
<ComboBoxItem Content="Laptop"/>
<ComboBoxItem Content="PC"/>
<ComboBoxItem Content="Stove"/>
<ComboBoxItem Content="Camera"/>
</ComboBox>
</Grid>
</Window>
Code:
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace WpfApp5
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Invoice invoice;
List<Invoice> Laptop = new List<Invoice>()
{
new Invoice(){Item ="0",Type="A",Price=10000},
new Invoice(){Item ="1",Type="B",Price=20000},
new Invoice(){Item ="2",Type="A",Price=15000},
};
public MainWindow()
{
InitializeComponent();
}
private class Invoice
{
public string Item { get; set; }
public string Type { get; set; }
public double Price { get; set; }
}
private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//Intercept the selected name
string sel = ComboBox1.SelectedItem.ToString();
string Rsel = sel.Substring(sel.IndexOf(" ") + 1);
if (Rsel == "Laptop")
{
DataGridInvoices.ItemsSource = Laptop;
}
}
}
}
Upvotes: 1