wpf - ItemsControl 中的 ToggleButtons 绑定(bind)到 ObservableCollection

标签 wpf itemscontrol togglebutton

首先,我想原谅我的英语。

我想要实现的目标看起来很简单,但我在实现中有点迷失。

背景:我有一个可观察的联系人集合。这些联系人都有 1 个或多个 ContactRole。我将联系人绑定(bind)到 ItemsControl 的 itemssource,并希望为联系人中的每个角色显示一个 ToggleButton。

问题:我的第一个问题是如何从具有角色的联系人列表转到屏幕上的大量切换按钮。我的第二个问题是,如果我单击一个切换按钮,则还需要检查具有相同联系人的所有其他按钮。如果我单击属于另一个联系人的另一个切换按钮,则需要取消选中所有选中的按钮,并且需要选中属于新联系人的按钮。

我现在拥有什么:我现在拥有的是 itemscontrol 中的 itemscontrol 和内部 itemscontrol,它的 itemtemplate 正在打印 ToggleButtons,如下所示的代码:

<Button Content="Add" Width="72" Height="27" Command="{Binding Path=AddContact}" VerticalAlignment="Top"/>
            <ItemsControl ItemsSource="{Binding Path=Contacts}" IsTabStop="False" Name="Parent">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel  />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <ItemsControl ItemsSource="{Binding ContactRoles}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <WrapPanel  />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <ToggleButton Content="{Binding}" CommandParameter="{Binding ElementName=Parent, Path=DataContext.Item}" Template="{StaticResource toggleButtonTemplateButtonBar}"
                                      Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.ViewContact}" Height="27" MinWidth="100">

                                    </ToggleButton>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

这部分代码正在显示。 我希望有人能帮助我解决这个问题。

我的其他一些问题是我是否需要创建一个继承自 ItemsControl 的自定义控件,或者可以通过模板和样式来完成吗?

如果您需要更多信息,请告诉我。

谢谢乔迪

编辑:

很抱歉,我不太清楚地表达了我的问题。回来评论你。第一个 ItemsControl 的 ItemsSource 保存一个包含唯一联系人的列表,第二个 ItemsControl 的 ItemsSource 保存属于该联系人的字符串(角色)列表。我想为所有联系人的每个角色显示一个切换按钮。但我认为您已经从我的代码示例中了解到了这一点。

Some more information

这张图片将显示我正在尝试做的事情。 我希望这能让事情变得更清楚。

最佳答案

正如 Snowbear 所说,请提供更多意见...从您的问题中我看到...

My first question is how can i go from a list of contacts with roles to a lot of ToggleButtons on screen.

GO是什么意思?您是否想知道 ContactsContact.Roles 如何转换为 ToggleButtons?这就是您的 ItemTemplate 正在做的事情。如果您要求切换按钮保留 Contact 对象中的某些属性或数据,那么您已经在 ItemTemplate 中使用了 Binding >.

        <ToggleButton Content="{Binding}" Tag="{Binding Roles}">
        </ToggleButton>

在上面的示例中,Tag 是 WPF 中 FrameworkElement 的非可视属性之一,它绑定(bind)到 Roles 列表code> 来自相应的 Contact 对象。

The second question i have is If i click one ToggleButton all other buttons that have the same contact need to be checked as well.

您是说在您的Contacts 列表中,某些Contact 对象在列表中添加了多次?如果是这样,那就是一个糟糕的设计,并且可能会在使用 ItemsSource 时导致错误。如果不是,那么您的所有其他具有**相同联系人**的按钮的声明会令人困惑。您的意思是您的联系人可能会重复,但通过引用它们不是同一对象。也许他们通过一些识别值(value)来共享,例如他们具有相同的 Contact.NameContact.ID 等。

如果联系人的某些标识值在不同的联系人对象中是相同的,那么您必须明智地使用 SelectedValue 绑定(bind)。

If i click another togglebutton which belong to another contact all checked buttons needs to be unchecked and the buttons belonging to the new contact needs to be checked.

一旦您决定真正想要做什么,即您是否多次添加相同的联系人对象,或者您有不同的Contact对象具有一些共同的,这也是可能的.

Do i need to make a Custom Control that inherits from ItemsControl or can this be done by templates and styles?

在 WPF 中,使用通用模板和样式可以实现任何目标。它们绝对消除了为各种视觉上相似的控件创建自定义控件的需要。

但是,如果您的控件具有需要在多个位置执行完全相同的行为或功能,并且希望将其密封并限制其自身执行该特定功能,那么创建自定义控件是有意义的。

因此,请重新表述您的问题并提供更清晰的输入。

关于wpf - ItemsControl 中的 ToggleButtons 绑定(bind)到 ObservableCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7649919/

相关文章:

C# WPF 应用程序在 VS 调试中运行,但不作为 EXE 运行

wpf - WPF中的复选框网格

javascript - 是/否按钮选择

android - 如何在android中设置动画切换按钮

wpf - 为什么不能将 OneWay 绑定(bind)到 ToggleButton IsChecked 属性?

c# - 如何将 CTRL + SHIFT + V 指定为按键手势?

wpf - 如何禁止选择列表框中的项目?

wpf - 如何使用突出显示的查询词在 WPF 项目控件中显示搜索结果

WPF 矩形 - 圆化顶角

.net - WPF 项目容器回收