0

这是一个带有动态标题和行的动态列表视图。但这里的问题是我尝试使用文本框作为

 xaml = "<DataTemplate><TextBox VerticalAlignment=\"Center\" TextChanged=\"{Binding " + propName + "}\" > " + propName + "</TextBox></DataTemplate>";

而不是在方法 CreateDataTemplate 中具有绑定的复选框,在单击 btn 后无法提取值。

这是带有 CheckBox 的代码。所以任何人都可以帮助我。我还需要文本框内的值。先感谢您

<Window x:Class="WpfListView.SalesPerson_SalesRegion_Association"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SalesPerson_SalesRegion_Association" Height="500" Width="500">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>
    <ListView Name="listView1" Width="400" Height="300" Margin="20" HorizontalAlignment="Left">
        <ListView.View>
            <GridView></GridView>
        </ListView.View>
    </ListView>

    <Button Grid.Row="1" HorizontalAlignment="Left" 
            Content="ShowSelectedMapping"
            Name="btnShow" Width="150" Margin="0,10,0,0" Click="btnShow_Click"></Button>

    <TextBlock Name="textBlock1" Grid.Row="2" 
                HorizontalAlignment="Left" Margin="0,10,0,0"></TextBlock>
</Grid>

 public partial class SalesPerson_SalesRegion_Association : Window
{
    public SalesPerson_SalesRegion_Association()
    {
        InitializeComponent();
        AddColumnsToListView();
        DataTable dt = DataHelper.GetRegionPersonAssociation();
        listView1.ItemsSource = dt.DefaultView;
    }

    private void btnShow_Click(object sender, RoutedEventArgs e)
    {
        DataView view = listView1.ItemsSource as DataView;
        DataTable userSelectionTbl = view.ToTable();

        DataTable idTable = DataHelper.GetRegionIdPersonIdMatrix();
        List<SalesRegion> lstRegion = SalesRegion.GetRegions();

        string selectedRegion = string.Empty;
        string msg = string.Empty;
        DataRow dRow=null;

        int totRows = userSelectionTbl.Rows.Count;
        int totCols = lstRegion.Count-1;
        string strTempMsg = string.Empty;
        bool isColChecked = false;
        for (int rowIndex = 0; rowIndex < totRows; rowIndex++)
        {
            dRow = userSelectionTbl.Rows[rowIndex];
            strTempMsg = dRow[0].ToString() + "(" + idTable.Rows[rowIndex][0].ToString() + ")" +  " : ";

            string rgnId="";
            isColChecked = false;
            foreach (SalesRegion region in lstRegion)
            {
                if (((bool)dRow[region.RegionName]) == true)
                {
                    rgnId = idTable.Rows[rowIndex][region.RegionName].ToString();
                    strTempMsg += region.RegionName + "(" + rgnId + ")";
                    isColChecked = true;                    }
            }
            if (isColChecked == false)
            {
                strTempMsg += "  : No region selected";
            }
            strTempMsg += Environment.NewLine;

            msg += strTempMsg;
        }

        textBlock1.Text = msg;
        string tt = "t";
    }
    private void AddColumnsToListView()
    {
        List<SalesRegion> lstSalesRegion = SalesRegion.GetRegions();
        List<SalesPerson> lstSalesPerson = SalesPerson.GetSalesPersons();

        GridViewColumn colSalesPerson = new GridViewColumn();
        colSalesPerson.Header = "Sales Person";
        colSalesPerson.DisplayMemberBinding = new Binding("SalesPersonName");
        colSalesPerson.Width = 150;
        GridView grdView = listView1.View as GridView;
        grdView.Columns.Add(colSalesPerson);

        //Since columns are dynamic we need a data template per column
        // in which we bind the checkBox's checked property with 
        //appropriate columnName
        Dictionary<string, DataTemplate> dict = GetDataTemplates(lstSalesRegion);

        foreach (SalesRegion region in lstSalesRegion)
        {
            GridViewColumn col1 = new GridViewColumn();
            col1.Header = region.RegionName;
            DataTemplate dTempl = dict[region.RegionName];
            col1.CellTemplate = dTempl;
            grdView.Columns.Add(col1);
        }
    }

    private Dictionary<string, DataTemplate> GetDataTemplates(List<SalesRegion> lstSalesRegion)
    {
        Dictionary<string, DataTemplate> dict = new Dictionary<string, DataTemplate>();
        foreach (SalesRegion region in lstSalesRegion)
        {
            DataTemplate dTemplate = CreateDataTemplate(region.RegionName);
            dict.Add(region.RegionName, dTemplate);
        }
        return dict;
    }
    private DataTemplate CreateDataTemplate(string propName)
    {
        MemoryStream sr = null;
        ParserContext pc = null;
        string xaml = string.Empty;
        xaml = "<DataTemplate><CheckBox VerticalAlignment=\"Center\" IsChecked=\"{Binding " + propName + "}\"></CheckBox></DataTemplate>";
        sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml));
        pc = new ParserContext();
        pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
        pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
        DataTemplate datatemplate = (DataTemplate)XamlReader.Load(sr, pc);
        return datatemplate;
    }

} // class ends here

} // DataHelper 类

public class SalesPerson
{
    public int SalesPersonId
    { get; set; }
    public string SalesPersonName
    { get; set; }

    public SalesPerson(int salesPersonId, string salesPersonName)
    {
        this.SalesPersonId = salesPersonId;
        this.SalesPersonName = salesPersonName;
    }

    public static List<SalesPerson> GetSalesPersons()
    {
        List<SalesPerson> lst = new List<SalesPerson>();
        lst.Add(new SalesPerson(101, "SalesPerson1"));
        lst.Add(new SalesPerson(201, "SalesPerson2"));
        lst.Add(new SalesPerson(301, "SalesPerson3"));
        lst.Add(new SalesPerson(401, "SalesPerson4"));
        lst.Add(new SalesPerson(501, "SalesPerson5"));
        return lst;
    }
} // class SalesPerson ends here

public class SalesRegion
{
    public int RegionId
    { get; set; }
    public string RegionName
    { get; set; }

    public SalesRegion(int regionId, string regionName)
    {
        this.RegionId = regionId;
        this.RegionName = regionName;
    }
    public static List<SalesRegion> GetRegions()
    {
        List<SalesRegion> lst = new List<SalesRegion>();
        lst.Add(new SalesRegion(501,"North"));
        lst.Add(new SalesRegion(502, "South"));
        lst.Add(new SalesRegion(503, "East"));
        lst.Add(new SalesRegion(504, "West"));
        lst.Add(new SalesRegion(505, "MyRegion"));
        return lst;
    }
} // class SalesRegion ends here

public class DataHelper
{
    public static DataTable GetRegionPersonAssociation()
    {
        DataTable dt = new DataTable();

        //Create data table structure
        // SalesPerson   Region1   Region2   Region3 ....
        DataColumn colSalesPerson = new DataColumn("SalesPersonName", typeof(string));
        dt.Columns.Add(colSalesPerson);

        List<SalesRegion> lstRegions = SalesRegion.GetRegions();
        DataColumn colRegion = null;
        foreach (SalesRegion region in lstRegions)
        {
            colRegion = new DataColumn(region.RegionName, typeof(bool));
            dt.Columns.Add(colRegion);
        }
        //Fill data into the data table
        List<SalesPerson> personList = SalesPerson.GetSalesPersons();
        DataRow dRow = null;
        foreach (SalesPerson sp in personList)
        {
            dRow = dt.NewRow();
            dRow["SalesPersonName"] = sp.SalesPersonName;
            foreach (SalesRegion sr in lstRegions)
            {
                dRow[sr.RegionName] = false;
            }
            dt.Rows.Add(dRow);
        }
        return dt;
    }

    public static DataTable GetRegionIdPersonIdMatrix()
    {
        DataTable dt = new DataTable();

        //Create data table structure
        // SalesPerson   Region1   Region2   Region3 ....
        DataColumn colSalesPerson = new DataColumn("SalesPersonId", typeof(int));
        dt.Columns.Add(colSalesPerson);

        List<SalesRegion> lstRegions = SalesRegion.GetRegions();
        DataColumn colRegion = null;
        foreach (SalesRegion region in lstRegions)
        {
            colRegion = new DataColumn(region.RegionName, typeof(int));
            dt.Columns.Add(colRegion);
        }
        //Fill data into the data table
        List<SalesPerson> personList = SalesPerson.GetSalesPersons();
        DataRow dRow = null;
        foreach (SalesPerson sp in personList)
        {
            dRow = dt.NewRow();
            dRow["SalesPersonId"] = sp.SalesPersonId;
            foreach (SalesRegion sr in lstRegions)
            {
                dRow[sr.RegionName] = sr.RegionId;
            }
            dt.Rows.Add(dRow);
        }
        return dt;
    }    } // class DataHelper ends here
4

1 回答 1

0

使用这样的文本框数据模板

xaml = "<DataTemplate><TextBox VerticalAlignment=\"Center\" Tag=\"{Binding " + propName + ", Mode=TwoWay}\" > " + propName + "</TextBox></DataTemplate>";

将文本框的样式添加到您的 listview.resources

            <Style TargetType="{x:Type TextBox}">
                <EventSetter Event="TextChanged" Handler="TextBox_TextChanged"></EventSetter>
            </Style>

将处理程序添加到您的代码隐藏中

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        (sender as TextBox).Tag = true;
    }

这将像您的复选框示例一样。

于 2012-08-09T09:20:48.120 回答