我有3个不同的DATA
将动态加载到_ListView
。
使用DATA1
,我需要添加更多列以在每一行中显示复选框控件。
我无法正常工作,输出显示CheckBox为String,如下所示
CS文件的详细信息:
public void DataLoad1()
{
var rowDT = data.GetListData();
string[] str = new string[rowDT.Columns.Count];
// loop data
foreach (DataRow row in rowDT.Rows)
{
for (int i = 0; i <= rowDT.Columns.Count - 1; i++)
{
str[i] = row[i].ToString();
}
// create checkbox
CheckBox chk = new CheckBox
{
// checkbox properties
Content = str[0],
IsChecked = false,
IsEnabled = true
};
_listView.Items.Add(
new
{
chkBox = chk,
DocNo = str[0],
QtyReq = str[1],
Price = help.ThousandSeparator(str[2]),
Date = help.ConvertDate(str[3]),
Status = help.ConvertStatus(str[4]),
Confirm = help.ConvertConfirmed(str[5])
}
);
}
}
这是XAML文件的详细信息:
<ListView x:Name="_listView" Height="365" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="BorderBrush" Value="LightGray" />
<Setter Property="BorderThickness" Value="0,0,1,1" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
是否可以在不编辑XAML文件的情况下实现这一目标?
答案 0 :(得分:1)
您必须在模板中添加复选框
var gridView = new GridView();
FrameworkElementFactory FEF = new FrameworkElementFactory(typeof(CheckBox));
FEF.AddHandler(CheckBox.ClickEvent, new RoutedEventHandler(CheckBox_CheckChanged), true);
Binding CBG = new Binding("DocNo");
FEF.SetBinding(CheckBox.ContentProperty, CBG);
Binding SBG = new Binding
{
Path = new PropertyPath("chkBox"),
Mode = BindingMode.OneWay,
};
FEF.SetBinding(CheckBox.IsCheckedProperty, SBG);
gridView.Columns.Add(new GridViewColumn()
{
Header = "CheckBox",
CellTemplate = new DataTemplate() { VisualTree = FEF }
});
gridView.Columns.Add(new GridViewColumn() { Header = "DocNo", DisplayMemberBinding = new Binding("DocNo") });
gridView.Columns.Add(new GridViewColumn() { Header = "QtyReq", DisplayMemberBinding = new Binding("QtyReq") });
gridView.Columns.Add(new GridViewColumn() { Header = "price", DisplayMemberBinding = new Binding("Price") });
_listView.View = gridView;
for (int i = 0; i < 5; i++)
{
_listView.Items.Add(
new
{
chkBox = true,
DocNo = i + "test",
QtyReq = i + "test",
Price = i + "test"
}
);
}
creating-wpf-check-list-box-using-c-sharp-code-behind
上述样品中使用测试数据,替换为原始数据
答案 1 :(得分:0)
这可能会帮助您实现所需的功能,尽管此处的逻辑不是很有效。
<ListView x:Name="_listView"
Height="365"
Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="BorderBrush" Value="LightGray" />
<Setter Property="BorderThickness" Value="0,0,1,1" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Content="{Binding DocNo}" IsChecked="{Binding Checked, Mode=OneWay}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding DocNo}" Header="Request Number" />
<GridViewColumn DisplayMemberBinding="{Binding QtyReq}" Header="Requested to UL" />
<GridViewColumn DisplayMemberBinding="{Binding Price}" Header="Price" />
<GridViewColumn DisplayMemberBinding="{Binding Date}" Header="Date" />
<GridViewColumn DisplayMemberBinding="{Binding Status}" Header="Status" />
<GridViewColumn DisplayMemberBinding="{Binding Confirm}" Header="Received" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
和cs
public void DataLoad1()
{
var rowDT = data.GetListData();
// loop data
foreach (DataRow row in rowDT.Rows)
{
var str = row.Select(o => o.ToString()).ToList();
_listView.Items.Add(new {
Checked = false,
DocNo = str[0],
QtyReq = str[1],
Price = help.ThousandSeparator(str[2]),
Date = help.ConvertDate(str[3]),
Status = help.ConvertStatus(str[4]),
Confirm = help.ConvertConfirmed(str[5])
});
}
}