2012-05-15 130 views
1

好吧,基本上我是C#的新手,我目前正在爲一家開發電子商務軟件的公司工作,而且我遇到了一個我似乎無法解決的問題。創建複選框列表

我有兩列,我想添加第三列,顯示覆選框的列表,但我只想顯示這些複選框時,其他兩個(或一個)字段顯示其中的某種形式的數據, IE:

  Name Type Check 
      lol lol 

我也希望這些複選框被自動打勾。

另一個問題是,我也想這樣做,當檢查產品顯示在我們的搜索,但如果他們未點擊,我希望他們被隱藏,但不會被刪除。

我目前使用的是GridView,因爲我不想重寫已經存在的其餘部分,因爲它與SQL數據庫進行通信,而我還不瞭解這些數據庫。

我們沒有使用ASP,我們正在使用XAML & C#(這兩者我都知之甚少)。下面的圖片是它需要的樣子的壞圖。

+1

你用什麼方法來設置你的應用程序中的值...你使用的是DataBinding嗎?直接設置值?另外,你說XAML,但是什麼類型:Silverlight,WIndows 8,WPF? – Robaticus

+0

我正在使用WPF,至於我們如何設置值,我相信我們正在使用Databinding,我對分鐘的C#語言不是很熟悉,因此我爲什麼掙扎了一點,工作是基於關掉一個,學習如我去基礎。 ^。^ –

回答

0

你提的問題是非常廣泛的,而且有些難以回答。簡而言之,您要查看的是控件上的Visibility屬性。

通過將Visibility設置爲Collapsed,UI將不會顯示該元素。如果需要,您可以根據另一個XAML元素的值或數據刪除來設置Visibility,但是您需要實現一個實現IValueConverter的類來進行轉換。

其中一個最常見的值轉換器是「布爾到可見性」轉換器。如果你在互聯網上搜索,你可以找到這些例子。您可以複製該方法並創建一個「EmptyToVisibilityConverter」或「NullToVisibilityConverter」或其他任何您需要的內容。一旦擁有該轉換器,您只需在綁定中將其指定爲可見性即可。例如:

<Page.Resources> 
    <conv:NullToVisibilityConverter x:Key="NullToVis"/> 
</Page.Resources> 

<CheckBox ... Checked={Binding ThisBoxIsChecked} 
       Visibility={Binding SomeOtherValue, 
          Converter={StaticResource NullToVis}}"/> 

請記住,能見度性能控制的內容之間的數據綁定的,並且需要是不一樣的。您可以將您的內容綁定到一個值,並可以看到另一個值。

如果你沒有使用數據綁定,你將不得不在代碼隱藏中設置它們。但爲什麼你不使用數據綁定?

編輯:這是一個工作示例。

如果這不是你要找的是什麼,然後我被鈍和不理解的問題

我建議你開始一個空白的項目,扔了進去這一點,和玩用它來了解如何設置。 XAML的學習曲線相對陡峭,通常有幾種方法可以完成你所需要的工作,但你真的需要對數據綁定和INotifyPropertyChanged(我在這個例子中沒有涉及到)的基本理解。

這裏的C#代碼:

using System; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Globalization; 
using System.Windows; 
using System.Windows.Data; 

namespace CheckboxList 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      //Create a viewmodel and add some data to it. 
      var viewModel = new MyViewModel(); 
      viewModel.Items.Add(new Data() {Name = "lol", Type = "lol", Selected = true}); 
      viewModel.Items.Add(new Data() { Name = "lol", Type = "not_lol", Selected = true }); 
      viewModel.Items.Add(new Data() { Name = "not_lol", Type = "not_lol", Selected = true }); 

      //Set the window's datacontext to the ViewModel. This will make binding work. 
      this.DataContext = viewModel; 

     } 
    } 

    //This is the ViewModel used to bind your data 
    public class MyViewModel 
    { 
     //This could just be a List<Data> but ObservableCollection<T> will automatically 
     //update your UI when items are added or removed from the collection. 
     public ObservableCollection<Data> Items { get; set; } 

     public MyViewModel() 
     { 
      Items = new ObservableCollection<Data>(); 
     } 
    } 

    //Just a sample class to hold the data for the grid. 
    //This is the class that is contained in the ObservableColleciton in the ViewModel 
    public class Data 
    { 
     public string Name { get; set; } 
     public string Type { get; set; } 
     public bool Selected { get; set; } 
    } 


    //This is an example converter. It looks to see if the element is set to "lol" 
    //If so, it returns Visibility.Collapsed. Otherwise, it returns Visibility.Visible. 
    public class LolToVisibilityConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      if (value != null && value is string) 
      { 
       var input = (string) value; 
       if (string.Equals(input, "lol", StringComparison.CurrentCultureIgnoreCase)) 
       { 
        return Visibility.Collapsed; 
       } 
       else 
       { 
        return Visibility.Visible; 
       } 
      } 

      return Visibility.Visible; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
} 

而XAML:

<Window x:Class="CheckboxList.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:CheckboxList="clr-namespace:CheckboxList" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <!-- Create an instance of LolToVisibilityConverter and call it "LolToVis" --> 
     <CheckboxList:LolToVisibilityConverter x:Key="LolToVis"/> 
    </Window.Resources> 
    <Grid> 

     <ListView ItemsSource="{Binding Items}"> <!--Bind the contents of the Items collection in our viewmodel --> 
      <ListView.View> 
       <GridView> 
        <GridViewColumn Width="140" Header="Name" DisplayMemberBinding="{Binding Name}"/> <!-- bind this element to this column--> 
        <GridViewColumn Width="140" Header="Type" DisplayMemberBinding="{Binding Type}"/> <!-- bind this element to this column--> 
        <GridViewColumn Width="140" Header="Selected" > <!-- because we don't want this to just display true/false, we need to set up a template--> 
         <GridViewColumn.CellTemplate> 
          <DataTemplate> 
           <!-- we set the Visibility property to Name, and the converter to LolToVis--> 
           <!-- whenever this field will be displayed, it calls the converter to convert the string to a Visibility value--> 
           <!-- The visibility value is checked to determine whether or not the element should be displayed--> 
           <CheckBox IsChecked="{Binding Selected}" Visibility="{Binding Name, Converter={StaticResource LolToVis}}"/> 
          </DataTemplate> 
         </GridViewColumn.CellTemplate> 
        </GridViewColumn> 
       </GridView> 
      </ListView.View> 
     </ListView> 
    </Grid> 
</Window> 
+0

謝謝你的回覆,但我在OP中不夠清楚,對此很抱歉。 –

+0

Woops,在我完成之前剪掉。 該軟件轉發到客戶網站,我想隱藏網站上的數據(他們希望隱藏/顯示的產品),而不是我們自己的個人用戶界面,我目前正在查看的部分是允許他們設置特別優惠組合IE,所有巧克力棒的折扣率和能夠挑選哪些巧克力棒顯示,並能夠打開該貓以供將來參考,但在一分鐘內主要問題是複選框,因爲一旦我完成了它們我可以向我的老闆展示一些進展:p –

0

這是我在過去用來爲每個datagridview的行中的複選框...然後我使用datagridview_cellcontentclick事件處理程序在單擊時更改複選框的值。在下面的代碼中,我有一個自定義類,它包含一個程序名,一個窗口標題和一個打開的文件或url。然後我創建了一個全局列表「oplist」,它是類型自定義Custructor的類型。然後,當我將列表中的項目添加到datagridview時,我指定了列標題。然後,從datagridview中添加或刪除任何內容變得非常容易。您只需刪除項目或將其添加到列表中,然後刷新datagridview。

public void addOpenProgramsToDataGrid() 
    { 
     dataGridView1.ColumnCount = 3; 

     DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn(); 
     { 
      column.HeaderText = "Selected"; 
      column.Name = "Selected"; 
      column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; 
      column.FlatStyle = FlatStyle.Standard; 
      column.ThreeState = false; 
      column.CellTemplate = new DataGridViewCheckBoxCell(); 
      column.CellTemplate.Style.BackColor = Color.White; 
     } 

     dataGridView1.Columns.Insert(0, column); // This is to be a checkbox column 
     dataGridView1.Columns[0].HeaderText = "X"; 


     dataGridView1.Columns[1].HeaderText = "Process Name:"; 

     dataGridView1.Columns[2].HeaderText = "Window Title"; 
     dataGridView1.Columns[3].HeaderText = "Open File or URL"; 

     dataGridView1.RowCount = opList.Count; 
     //opList.RemoveRange(0, opList.Count); 
     for (int a = 0; a < opList.Count; a++) 
     { 
      openProgram tempProgram = new openProgram(); 
      tempProgram = opList[a]; 
      dataGridView1.Rows[a].Cells[0].Value = true; 

      dataGridView1.Rows[a].Cells[1].Value = tempProgram.processName; 
      dataGridView1.Rows[a].Cells[2].Value = tempProgram.windowTitle; 
      dataGridView1.Rows[a].Cells[3].Value = tempProgram.openFileOrURL; 
     } 
     selectAllCheckBox.Checked = true; 
    } 
+0

這更容易用XAML和數據綁定聲明式處理。 – Robaticus