我有两个组合框( m 和 t ),每个框都包含一些项目。根据他们的组合,我会显示一个复选框列表。
要根据组合框中的选定项显示想要的复选框,我使用 updateList()方法。
要知道所有项目,并选择了m和t,我将它们作为全局变量
<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 组合然后返回,则无需记住其他添加的checboxes组合。
我想要像:
答案 0 :(得分:1)
使用WPF时应该真正使用MVVM模式。当您的项目变得更加复杂时,使用类似WPF的Windows窗体将使其仅与Windows窗体一样有效。你选择WPF来获得它与Windows Forms相比的所有功能,对吗?然后使用MVVM获得此功能。
至于你的按钮:你已经这样做了。单击该按钮时,只需添加您已经执行过的项目:
List.Add(new BoolStringClass { Selected = true, Texto = "NEW ENTRY" });