C# 如何在我的复选框中显示选中的项目
编辑: 我创建了三个列表框: 第一个列表框:C# 如何在我的复选框中显示选中的项目,c#,wpf,checkbox,data-binding,listbox,C#,Wpf,Checkbox,Data Binding,Listbox,编辑: 我创建了三个列表框: 第一个列表框:列表框,显示美国各州的列表 第二个列表框:ListBox\u Mirror,其中包含一个复选框,用于显示“ListBox”的选定项 第三个列表框:(无名称),用于显示“列表框”的选中项 第一个列表框->第二个列表框工作正常。但是,第二个列表框->第三个列表框不起作用,如下图所示: 程序: 在第一个列表框中选择所有四项 选中第二个列表框复选框中的前两项(California和Illioni) 查看第三个列表框中是否显示加利福尼亚和伊利奥尼 (在我的例子
列表框
,显示美国各州的列表
第二个列表框:ListBox\u Mirror
,其中包含一个复选框,用于显示“ListBox”的选定项
第三个列表框:(无名称)
,用于显示“列表框”的选中项
第一个列表框->第二个列表框工作正常。但是,第二个列表框->第三个列表框不起作用,如下图所示:
程序:
using System.Collections.ObjectModel;
namespace CheckedListBox
{
public class StateList
{
public ObservableCollection<string> Data { get; }
public StateList()
{
Data = new ObservableCollection<string>();
Data.Add("California");
Data.Add("Illinoi");
Data.Add("Michigan");
Data.Add("New York");
}
}
}
using System.Windows;
namespace CheckedListBox
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new StateList();
}
}
}
。。。我根据IntelliSense的建议更改了第三个列表框的绑定属性,但它也不起作用。
请告诉我如何解决这个问题。谢谢。两件事,检查的
没有绑定,因此没有设置任何内容。此外,您的数据只是字符串的引用;您应该将其更改为至少具有两个属性的类,一个是布尔属性,另一个是您现在拥有的字符串。然后适当地捆绑
下面是如何做到这一点。我有一个在代码隐藏中定义的模型,但是你可以从它的结构中得到想法
<Page ...
xmlns:model="clr-namespace:WPFStack.Model"/>
...
<Page.Resources>
<model:Orders x:Key="Orders">
<model:Order CustomerName="Alpha"
OrderId="997"
InProgress="True" />
<model:Order CustomerName="Beta"
OrderId="998"
InProgress="False" />
<model:Order CustomerName="Omega"
OrderId="999"
InProgress="True" />
<model:Order CustomerName="Zeta"
OrderId="1000"
InProgress="False" />
</model:Orders>
然后,我将订阅原始文件上的事件,例如已加载
、已选中
和未选中
<ListBox x:Name="lbOriginal"
ItemsSource="{StaticResource Orders}"
Loaded="ProcessChange">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel HorizontalAlignment="Stretch" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding CustomerName}"
IsChecked="{Binding InProgress, Mode=TwoWay}"
Checked="ProcessChange"
Unchecked="ProcessChange"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
然后是同步和镜像
您的描述中的数字有错误吗?否则,你为什么需要第三个列表框?@AlexButenko:我已经编辑了我的描述。对不起,我解释得不好。我刚刚发现,通过将Checked=“ShowState”
添加到
并添加private void ShowState(object sender,RoutedEventArgs args){MessageBox.Show(sender.ToString();},我可以看到IsChecked
复选框的状态
到后面的代码。让我看看是否可以继续。我可能会因为我解释得不好而把你弄糊涂了,对不起。您的代码可以工作,但似乎您完成了我的“第一个列表框->第二个列表框”部分。你能做我的“第二个列表框->第三个列表框”部分吗?非常抱歉,谢谢。谢谢,但我遇到一个错误“资源”订单“无法解决”。在ItemsSource=“{StaticResource Orders}”
。我在文件夹WPFStack\u Mirror\uuuΩmegaMan.Model
下添加了您的Orders
类。我必须添加xmlns:model=“clr名称空间:WPFStack\u Mirror\uuuΩmegaMan.model”
还是什么?很抱歉,我还是个初学者……您需要添加xmlns:model=
,但是名称空间必须映射到项目中它所在的位置。Intellisense应列出位置。但是你应该能够使用你自己的数据……是的,它是有效的!非常感谢。我只是忘了将
添加到我的XAML中。是我的错。我的错,对不起。
<ListBox ItemsSource="{StaticResource Orders}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel HorizontalAlignment="Stretch" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding CustomerName}"
IsChecked="{Binding InProgress, Mode=TwoWay}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
namespace WPFStack.Model
{
/// <summary>Class Orders which is a placeholder for Xaml example data.</summary>
public class Orders : List<Order> { }
public class Order
{
public string CustomerName { get; set; }
public int OrderId { get; set; }
public bool InProgress { get; set; }
}
}
<ListBox x:Name="lbShowSelected">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel HorizontalAlignment="Stretch" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding .}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox x:Name="lbOriginal"
ItemsSource="{StaticResource Orders}"
Loaded="ProcessChange">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel HorizontalAlignment="Stretch" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding CustomerName}"
IsChecked="{Binding InProgress, Mode=TwoWay}"
Checked="ProcessChange"
Unchecked="ProcessChange"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
private void ProcessChange(object sender, RoutedEventArgs e)
{
if (lbOriginal.ItemsSource is Orders asOrders)
{
lbShowSelected.ItemsSource = null; // Inform control of reset
lbShowSelected.ItemsSource = asOrders.Where(ord => ord.InProgress)
.Select(ord => ord.CustomerName)
.ToList();
}
}