将 ComboBox 中的值添加到 WPF 中的 DataGrid
Adding values that are in a ComboBox into a DataGrid in WPF
我有 4 个组合框,每个组合框都在下拉菜单中标记。之后,在我的代码中,我想为 ComboBox 设置一个 if else 语句。如果选择了该项目,则会根据某些值(成本、项目类型和项目名称)将其添加到 DataGrid 中。
ComboBox 的 XAML(针对此问题缩短)
<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>
这是数据网格XAML代码:
<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>
最后,这是 cs 文件中的代码,它为 DataGrid(发票)创建 class 和计算总计的方法(只是想知道如何将 ComboBox 设置为 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)
{
}
}
一旦我弄清楚了其中一个,剩下的就应该很简单了。如果需要更多信息,请告诉我,谢谢。
主要注意绑定:
<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>
删除重复列并删除最后一个空行:
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>
代码:
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;
}
}
}
}
我有 4 个组合框,每个组合框都在下拉菜单中标记。之后,在我的代码中,我想为 ComboBox 设置一个 if else 语句。如果选择了该项目,则会根据某些值(成本、项目类型和项目名称)将其添加到 DataGrid 中。
ComboBox 的XAML(针对此问题缩短)
<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>
这是数据网格XAML代码:
<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>
最后,这是 cs 文件中的代码,它为 DataGrid(发票)创建 class 和计算总计的方法(只是想知道如何将 ComboBox 设置为 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)
{
}
}
一旦我弄清楚了其中一个,剩下的就应该很简单了。如果需要更多信息,请告诉我,谢谢。
主要注意绑定:
<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>
删除重复列并删除最后一个空行:
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>
代码:
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;
}
}
}
}