我有两个comboBoxes (m和t),每个项目都有。根据它们的组合,我会显示一个复选框列表。
为了显示想要的复选框(取决于comboBoxes上选定的项),我使用了一个updateList()方法。
要知道所有项目,并选择m和t,我将它们设置为全局变量。
xaml
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ComboBox
HorizontalAlignment="Left"
Margin="93,12,0,0"
VerticalAlignment="Top"
Width="120"
Loaded="ComboBoxModulo_Loaded"
SelectionChanged="ComboBoxModulo_SelectionChanged"/>
<ComboBox
HorizontalAlignment="Left"
Margin="93,80,0,0"
VerticalAlignment="Top"
Width="120"
Loaded="ComboBoxTipo_Loaded"
SelectionChanged="ComboBoxTipo_SelectionChanged"/>
<ListBox ItemsSource="{Binding List}" Margin="318,12,12,22">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Selected}" Content="{Binding Texto}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Margin="318,5,5,5" Padding="3" Content="GET SELECTED INFO"
Grid.Row="1" Click="Button_Click"/>
<Label Content="M" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="labelModulo" VerticalAlignment="Top" />
<Label Content="T" Height="28" HorizontalAlignment="Left" Margin="12,74,0,0" Name="labelTipo" VerticalAlignment="Top" />
</Grid>
代码背后
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace mt
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
/*public MainWindow()
{
InitializeComponent();
}*/
public ObservableCollection<BoolStringClass> List { get; set; }
public string m;
public string t;
private void ComboBoxModulo_Loaded(object sender, RoutedEventArgs e)
{
List<string> mList = new List<string>();
mList.Add("m1");
mList.Add("m2");
var comboBox = sender as ComboBox;
comboBox.ItemsSource = mList;
comboBox.SelectedIndex = 0;
}
private void ComboBoxModulo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var comboBox = sender as ComboBox;
string mSelect = comboBox.SelectedItem as string;
m = mSelect;
updateList();
}
private void ComboBoxTipo_Loaded(object sender, RoutedEventArgs e)
{
List<string> tList = new List<string>();
tList.Add("t1");
tList.Add("t2");
var comboBox = sender as ComboBox;
comboBox.ItemsSource = tList;
comboBox.SelectedIndex = 0;
}
private void ComboBoxTipo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var comboBox = sender as ComboBox;
string tSelect = comboBox.SelectedItem as string;
t = tSelect;
updateList();
}
void updateList()
{
List.Clear();
if (m == "m1" && t == "t1")
{
List.Add(new BoolStringClass { Selected = true, Texto = "m1 t1" });
List.Add(new BoolStringClass { Selected = true, Texto = "m1 t1" });
}
else if (m == "m1" && t == "t2")
{
List.Add(new BoolStringClass { Selected = true, Texto = "m1 t2" });
List.Add(new BoolStringClass { Selected = true, Texto = "m1 t2" });
}
else if (m == "m2" && t == "t1")
{
List.Add(new BoolStringClass { Selected = true, Texto = "m2 t1" });
List.Add(new BoolStringClass { Selected = true, Texto = "m2 t1" });
}
else if (m == "m2" && t == "t2")
{
List.Add(new BoolStringClass { Selected = true, Texto = "m1 t2" });
List.Add(new BoolStringClass { Selected = true, Texto = "m1 t2" });
}
this.DataContext = this;
}
public MainWindow()
{
InitializeComponent();
List = new ObservableCollection<BoolStringClass>();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//Get a List<BoolStringClass> that contains all selected items:
var res = (
from item in List
where item.Selected == true
select item
).ToList<BoolStringClass>();
//Convert all items to a concatenated string:
var res2 =
from item in List
select item.Texto + (item.Selected ? " selected." : " NOT selected.");
MessageBox.Show("title:\r\n\r\n" +
string.Join("\r\n" + "m: "+m + " t:" + t, new List<string>(res2).ToArray()));
}
}
public class BoolStringClass : INotifyPropertyChanged
{
public string Texto { get; set; }
//Provide change-notification for Selected
private bool _fIsSelected = false;
public bool Selected
{
get { return _fIsSelected; }
set
{
_fIsSelected = value;
this.OnPropertyChanged("Selected");
}
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string strPropertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName));
}
#endregion
}
}这样做的结果是:

但是,我希望在复选框列表的末尾包含一个按钮,所以当单击它时,它会向列表中添加其他复选框(可能是询问复选框的名称.)
如果用户决定其他、m、和t组合,然后返回,则不必记住其他添加的复选框组合。
我想要的是:

发布于 2015-03-11 06:57:57
在使用WPF时,您确实应该使用MVVM模式。当项目变得更加复杂时,像使用Windows窗体一样使用WPF只会使其与Windows窗体一样有效。你选择了WPF,因为它与Windows窗体相比有多强大,对吗?然后使用MVVM使这个电源可用。
至于你的按钮:你已经这么做了。单击按钮时,只需添加一个项目,就像您已经做的那样:
List.Add(new BoolStringClass { Selected = true, Texto = "NEW ENTRY" });https://stackoverflow.com/questions/28980233
复制相似问题