gpt4 book ai didi

c# - 将复选框动态插入到wpf中的复选框列表的按钮

转载 作者:太空宇宙 更新时间:2023-11-03 13:10:30 26 4
gpt4 key购买 nike

我有两个组合框(mt),每个组合框都有一些项目。根据它们的组合,我显示了一个复选框列表。

为了根据组合框上的选定项目显示想要的复选框,我使用了 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
}
}

结果:

enter image description here

但是我想在复选框列表的末尾包含一个按钮,所以当它被点击时它会添加其他复选框到列表中(可能会询问复选框的名称......)

如果用户决定其他 mt 组合然后返回,则无需记住其他添加的复选框组合。

我想要这样的东西:

enter image description here

最佳答案

在使用 WPF 时,您应该真正使用 MVVM 模式。当您的项目变得更加复杂时,像使用 Windows 窗体一样使用 WPF 将使其仅与 Windows 窗体一样有效。与 Windows 窗体相比,您选择 WPF 是因为它具有所有强大功能,对吗?然后使用 MVVM 来获得这种能力。

至于您的按钮:您已经这样做了。单击该按钮时,只需像您已经做的那样添加一个项目:

List.Add(new BoolStringClass { Selected = true, Texto = "NEW ENTRY" });

关于c# - 将复选框动态插入到wpf中的复选框列表的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28980233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com