我刚刚遇到了一个问题,即在ItemsControl中将集合绑定到包含有界ComboBox的ItemTeplate.
在我的场景中,我需要"生成"表单,其中包含集合中每个项目的文本框和组合框,并允许用户更新项目.我可以使用DataGrid,但我想在编辑模式下看到所有行,所以我使用ItemsControl和自定义ItemTemplate.
编辑文本框是可以的,但是当您尝试更改任何ComboBox时,其他行中的所有其他ComboBox也会更改.这是一个错误或功能吗?
谢谢,Ondrej
Window.xaml
<Window x:Class="ComboInItemsControlSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="480" Width="640">
<Window.Resources>
<CollectionViewSource x:Key="cvsComboSource"
Source="{Binding Path=AvailableItemTypes}" />
<DataTemplate x:Key="ItemTemplate">
<Border BorderBrush="Black" BorderThickness="0.5" Margin="2">
<Grid Margin="3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="20" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Text="{Binding Path=ItemValue}" />
<ComboBox Grid.Column="2"
SelectedValue="{Binding Path=ItemType}"
ItemsSource="{Binding Source={StaticResource cvsComboSource}}"
DisplayMemberPath="Name"
SelectedValuePath="Value" />
</Grid>
</Border>
</DataTemplate>
</Window.Resources>
<Grid>
<ItemsControl ItemsSource="{Binding Path=SampleItems}"
ItemTemplate="{StaticResource ItemTemplate}"
Margin="10" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
Window.xaml.cs
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataContext = new …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写 SQL 语句,仅当目标表中不存在该行时,才将带参数的多行批量插入到表中。
我在如何将参数标记传递到 SQL 查询中时遇到问题。当我使用下面的代码时,出现异常: “不允许 SQL0584 NULL 或 VALUES 中的参数标记。”
using (var conn = new iDB2Connection(_connectionString)) {
await conn.OpenAsync();
using (var tran = conn.BeginTransaction()) {
using (var cmd = conn.CreateCommand()) {
cmd.Transaction = tran;
cmd.CommandText = @"
MERGE INTO TableXYZ AS mt
USING (
VALUES(@column1, @column2)
) AS vt(Column1, Column2)
ON (
mt.Column1 = vt.Column1 AND mt.Column2 = vt.Column2
)
WHEN NOT MATCHED THEN
INSERT (Column1, Column2) VALUES (vt.Column1, vt.Column2)
";
cmd.DeriveParameters();
foreach (var item in items) { …Run Code Online (Sandbox Code Playgroud)