如何在我的CheckBox中显示选中的项目

已编辑

我创建了三个列表框:

第一个列表框:listBox,其中显示了美国各州的列表

第二个列表框:listBox_Mirror,其中包含一个复选框,用于显示“列表框”的选定项

第三个列表框:(No Name),应该显示“ listBox_Mirror”的选中项

第一个ListBox->第二个ListBox可以正常工作。但是,第二张列表框->第三张列表框不起作用,如下图所示:

如何在我的CheckBox中显示选中的项目

程序:

  1. 在第一个列表框中选择所有四个项目。

  2. 检查第二个ListBox的CheckBox中的前两个项目(加利福尼亚和伊利奥尼)。

  3. 查看第三列表框中是否显示了加利福尼亚和伊利奥尼。

(在我的情况下,什么都没有显示。)

这是我的代码:

StateList.cs

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");
        }
    }
}

MainWindow.xaml

<Window x:Class="CheckedListBox.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:CheckedListBox"
    mc:Ignorable="d"
    Title="MainWindow" Height="500" Width="400">
<Grid>
    <ListBox ItemsSource="{Binding Path=Data}" x:Name="listBox" Margin="100,50,100,300" Selectionmode="Multiple"/>
    <ListBox ItemsSource="{Binding SelectedItems,ElementName=listBox}" x:Name="listBox_Mirror" Margin="100,200,150">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <CheckBox Content="{TemplateBinding Content}"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
    <ListBox ItemsSource="{Binding SelectedItems,ElementName=listBox_Mirror}" Margin="100,350,25"/>
</Grid>

MainWindow.xaml.cs

using System.Windows;

    namespace CheckedListBox
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                this.DataContext = new StateList();
            }
        }
    }

...我已经根据IntelliSense的建议更改了第三个ListBox的Binding属性,但是它也不起作用。 请告诉我如何解决此问题。谢谢。

sja811024 回答:如何在我的CheckBox中显示选中的项目

有两件事,IsChecked没有绑定,因此什么也没设置。同样,您的数据只是字符串的引用;您应该将其更改为至少具有两个属性的类,一个属性为布尔值,另一个属性为您现在拥有的字符串。然后适当绑定。


这是方法。我有一个模型,该模型在后面的代码中定义,但是您可以在其结构上了解它。

  <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 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>

运行它将显示以下内容:

enter image description here

模型

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; }
    }
}

镜像

好吧,现在我将命名控件lbOriginallbSelected以便在后面的代码中进行访问。新控件lbSelected将这样镜像,而无需直接连接到lbOriginal控件数据:

<ListBox x:Name="lbShowSelected">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel HorizontalAlignment="Stretch" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding .}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

然后,我将订阅原件上的事件,例如LoadedCheckedUnChecked

<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>

因此,在每个步骤ProcessChange中,方法都会正确更新镜像(我称之为镜像):

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();
    }
}

然后它已同步并镜像

enter image description here

本文链接:https://www.f2er.com/3131733.html

大家都在问