ItemsControl 中的 ToggleButtons 绑定到 ObservableCollection

发布于 2024-12-08 06:02:42 字数 2712 浏览 0 评论 0原文

首先我想原谅我的英语。

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

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

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

我现在有什么:我现在有的是itemscontrol中的itemscontrol,它的itemtemplate内部itemscontrol正在打印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 保存属于该联系人的字符串(角色)列表。我想为所有联系人的每个角色显示一个切换按钮。但我认为您已经从我的代码示例中了解到了这一点。

一些更多信息

此图片将显示我正在尝试执行的操作。 我希望这能让事情变得更清楚。

First of all i want to excuse my English.

What i want to achieve looks very simple but i'm a bit lost in the implementation.

Background: I have an ObservableCollection of Contacts. these contacts all have 1 or more ContactRoles. I bind the contacts to the itemssource of an ItemsControl and want a ToggleButton for every role in the contact to be displayed.

Question: My first question is how can i go from a list of contacts with roles to a lot of ToggleButtons on screen. 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. 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.

What do i have now: What i have now is an itemscontrol in an itemscontrol and the internal itemscontrol it's itemtemplate is printing the ToggleButtons look an the code below:

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

This part of the code is showing.
I hope someone can help me with this.

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

I you need more information let me know.

Thanks, Jordy

EDIT:

I'm sorry i was not so clear with formulating my questions. to come back on you comment. The ItemsSource of the first ItemsControl hold a list with unique contacts, the ItemsSource of the second hold a list of strings (roles) that belong to this contact. I want to show an ToggleButton for each role of all contacts. But i think you've guested that from my codeexample.

Some more information

This image will show what i'm trying to do.
I hope this makes thing more clear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

執念 2024-12-15 06:02:42

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

我的第一个问题是如何从具有角色的联系人列表中查找
屏幕上有很多切换按钮。

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

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

在上面的示例中,Tag 是 WPF 中 FrameworkElement 的非可视属性之一,它绑定到来自以下位置的 Roles 列表:相应的Contact对象。

我的第二个问题是如果我单击一个 ToggleButton 所有其他按钮
具有相同联系人的按钮也需要检查。

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

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

如果我单击属于另一个联系人的另一个切换按钮
选中的按钮需要取消选中,并且属于
需要检查新联系人。

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

我是否需要创建一个继承自的自定义控件
ItemsControl 还是可以通过模板和样式来完成?

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

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

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

As Snowbear said please provide more inputs... From your questions I see ...

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

What do you mean by GO? Are you asking that how are Contacts or Contact.Roles tranformed into ToggleButtons? Then that is what your ItemTemplate is doing. If you are asking that you want some property or data from Contact object to be held by the toggle button then you have already used Binding in your ItemTemplate.

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

In the above example, Tag which is one of the non-visual properties of FrameworkElements in WPF, is bound to the list of Roles from the corresponding Contact object.

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.

Are you saying that in your list of Contacts some Contact object is added multiple times in the list? If so that is a bad design and may cause blunders while using ItemsSource. If no, then this statement of yours all other buttons that have the **same contact** is confusing. Do you mean that you have Contacts which may repeat but they are not the same object by reference. Probably they share by some identifying value e.g. they have same Contact.Name or Contact.ID etc.

If some identifying value of contact is what is same among different contact objects then you will have to intelligently use the SelectedValue binding.

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.

Again this is possible once you decide what are you really trying to do i.e. are you adding same Contact object multiple time or you have different Contact objects having some value common.

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

in WPF, ANYTHING can be achieved using common templates and styles. They definitely eliminate need for creating a custom control for various visually similar looking controls.

BUT if you control has a behavior or functionality that you need to perform exactly the same in multiple places and you want it sealed and to limit itself performing that specific function, then creating a custom control makes sense.

So please rephrase your question and provide clearer inputs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文