2012-05-15 134 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