0

我是 mvvm 的新手,并尝试学习。所以我创建了新项目并学习,但是:我对 Silverlight 的复选框有疑问(花了两天时间)我不知道如何在 silverlight mvvm 中绑定复选框。

在我的 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;
    }
}

当我开始我的简单项目复选框时未选中。

最终结果:

最终结果

有什么帮助吗?谢谢。雅各布斯

4

1 回答 1

0

按照您的要求,如果我理解正确,您将显示Category特定年份的仓库中是否存在给定的内容。我不知道你为什么OnWarehouseYearSummary这里有列表,但考虑到你的每一行都DataGrid代表,SaleData并且你每年根据YearSummary你的列表添加列,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);
于 2013-09-08T11:37:57.040 回答