2012-08-08 91 views
0

這是一個動態列表視圖,帶有動態標題和行。但這裏的問題是,文我嘗試使用文本框作爲帶有文本框的二維動態列表視圖

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

,而不是一個複選框,在CreateDataTemplate點擊幹訓局後值不能extratcted方法的結合。

這裏是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 
+0

小心:你將屬性綁定到TextChanged =「{Binding」+ propName +「} – michele 2012-08-08 14:54:42

+0

的事件那麼我該如何去檢查它?在結束時點擊按鈕,我需要沿着文本框的值顯示出watever的輸出 – rohit 2012-08-09 06:45:56

+0

你是否需要textboxes,因爲你希望用戶能夠編輯區域名稱?無論如何我建議你去看看ObservableCollection類 – michele 2012-08-09 08:08:23

回答

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; 
    } 

這會像你的複選框例子。

+0

非常感謝。非常感謝您的幫助。我仍然有這個問題。我如何從編輯的文本框中獲取值。 – rohit 2012-08-09 10:17:22

+0

您需要升級您綁定到每個單元格的對象。不是簡單的布爾值,而是像bool |這樣的結構字符串,所以你可以將布爾值綁定到文本框的Tag屬性,並將字符串綁定到Text屬性(請考慮將回答標記爲已接受) – michele 2012-08-09 10:30:10

+0

對不起,打擾了你,但我gtgt答案。真的很高興幫助你。再次感謝你。 – rohit 2012-08-09 10:32:23