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

伊恩·哈克

编辑:

我创建了三个ListBoxes:

1st ListBox:,listBox显示美国各州的列表

第二个ListBox:,listBox_Mirror其中包含一个CheckBox来显示“ listBox”的选定项

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

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

在此处输入图片说明

程序:

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

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

  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,100,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,100,25"/>
</Grid>

MainWindow.xaml.cs

using System.Windows;

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

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

欧米茄

两件事没有绑定到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>

运行它显示以下内容:

在此处输入图片说明

模型

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>

然后,我将订阅诸如之类的事件LoadedChecked订阅UnChecked原始事件

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

然后同步并镜像

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Crystal Report中显示选中的CheckBox

如何在uwp中的ListView的所选项目上显示选中的复选框

如何在IOS中显示选中的选中的TableViewCell(目标c)

如何在 JavaFx 中的 CheckBoxTreeView 中获取选中的项目

如何在CheckListBox中获取选中的项目字符串

如何在NavigationView中设置默认的选中项目?

我如何在我的 RecyclerView 中烘烤选中的框的数量?

我如何在Azure Devops工作项目屏幕中显示所有工作项目

如何在我的Recyclerview中实现CheckBox?

如何在Feedzy中显示项目属性

如何在字典中显示多个项目?

如何在换行中显示每对项目?

当numColumns更改时,如何在gridview上保留选中的项目突出显示?

我如何在购物车图标后面显示购物车中的总项目

我如何在控制台C#中显示带有项目的List <object>

如何在导航视图中取消选中已选中的项目?

如何在取消选中 Angular 中的 mat-checkbox 时重置输入文件上传的值?

如何在Angular 7中将mat-checkbox标记为已选中

如何在React中从购物车状态中删除未选中的项目

如何在 v-checkbox 中显示头像 url?

如何取消选中AlertDialog(setMultiChoiceItems)中的项目?

如何从列表中删除选中的项目?

如何从WPF中的combobox获取选中的项目?

我如何确定CheckedListBox中的单个项目是否被选中?C#

如何在qml中的ListView中显示列表中的项目

如何在 rails 中显示我的同事?

如何在我的 ggplot 中显示图例?

我们如何在Android的上下文操作栏中显示后退按钮而不是完成按钮(选中标记)

在选中和删除项目时如何在react-select中捕获添加和删除事件?