我是mvvm的新手,并尝试学习。所以我创建了新的项目和学习,但是:我遇到了带有复选框的Silverlight问题(花了两天时间)我不知道,如何在silverlight mvvm中绑定checbox。
在我的xaml文件中,我有
<sdk:DataGrid x:Name="List_ServicessProcess" AutoGenerateColumns="False">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Summary[0].Total}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Summary[0].Total}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
在我的C#代码中:
public class SaleData
{
public int Categoryid { get; set; }
public string Category { get; set; }
public List<YearSummary> Summary { get; set; }
}
public class YearSummary
{
public int Year { get; set; }
public double Total { get; set; }
public List<OnWarehouse> Warehouse { get; set; }
}
public class OnWarehouse
{
public bool yes { get; set; }
}
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
var data = GetData();
List_ServicessProcess.ItemsSource = data;
var firstRow = data.First();
for (int i = 0; i < firstRow.Summary.Count; i++)
{
var monthlySummary = firstRow.Summary[i];
List_ServicessProcess.Columns.Add(CreateColumn(i, monthlySummary.Year.ToString()));
}
}
public static List<SaleData> GetData()
{
List<SaleData> data = new List<SaleData>();
data.Add(new SaleData() { Category = "Laptop Toshiba xyz", Summary = GetSummaryData() });
data.Add(new SaleData() { Category = "Laptop Toshiba xyz", Summary = GetSummaryData() });
data.Add(new SaleData() { Category = "Laptop Toshiba yzx", Summary = GetSummaryData() });
data.Add(new SaleData() { Category = "Laptop Toshiba yzx", Summary = GetSummaryData() });
data.Add(new SaleData() { Category = "Laptop Toshiba yzx", Summary = GetSummaryData() });
return data;
}
private static List<YearSummary> GetSummaryData()
{
List<YearSummary> data = new List<YearSummary>();
Random rnd = new Random();
for (int i = 0; i < 5; i++)
{
var summary = new YearSummary() { Total = rnd.Next(-5000, 10000), Year = 2008 + i, Warehouse = GetWarehouse() };
data.Add(summary);
}
return data;
}
private static List<OnWarehouse> GetWarehouse()
{
List<OnWarehouse> data = new List<OnWarehouse>();
Random rnd = new Random();
for (int i = 0; i < 4; i++)
{
var summary = new OnWarehouse() { yes = (i % 2 == 0)};
data.Add(summary);
}
return data;
}
private DataGridTemplateColumn CreateColumn(int index, string header)
{
string cellTemp = string.Format(@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
<CheckBox IsChecked=""{{Binding Path=Warehouse[{0}].yes,Mode=TwoWay}}""/>
</DataTemplate>", index);
DataGridTemplateColumn column = new DataGridTemplateColumn();
column.Header = header;
column.CellTemplate = (DataTemplate)XamlReader.Load(cellTemp);
return column;
}
}
当我启动我的简单项目时,不会选中复选框。
最后结果:
有任何帮助吗?谢谢。 雅各布
答案 0 :(得分:0)
根据您的要求,如果我正确理解它,您将显示特定年份仓库中是否存在给定的Category
。我不知道为什么你在OnWarehouse
YearSummary
列出DataGrid
,但考虑到SaleData
中的每一行代表YearSummary
而你每年都会添加一列根据{{1}}中SalesData
的列表,xaml不应该像
string.Format(@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
<CheckBox IsChecked=""{{Binding Path=Summary[{0}].Warehouse[0].yes,Mode=TwoWay}}""/>
</DataTemplate>", index);