如何将ToggleButtons与列表绑定? [英] How to bind ToggleButtons with a List?
问题描述
我有很多ToggleButtons(约260)。部分代码与ToggleButtons:
I have a lot of ToggleButtons (about 260). Part of my code with ToggleButtons:
<ToggleButton Style="{DynamicResource Seat}" Content="10" Click="OnSeatButtonClick"/>
<ToggleButton Style="{DynamicResource Seat}" Content="18" Click="OnSeatButtonClick"/>
<ToggleButton Style="{DynamicResource Seat}" Content="10" Click="OnSeatButtonClick" IsEnabled="False"/>
我想添加动作,点击按钮确认更改,当前的ToggleButtons检查将进入禁用状态。而且我还希望将这些更改保存到数据库。
I want to add action that after clicking on button "Confirm changes", ToggleButtons that are at the moment checked will go into disabled state. And what is more I want those changes to be saved to database.
所以我想我需要为这些ToggleButtons添加一些唯一的ID。然后以某种方式将它们绑定到具有类型为Saet的元素的列表。
So I figured that I would need to add some unique id to those ToggleButtons. And then somehow bind them to a List with elements of type Saet.
座位代码:
public class Seat
{
string Column;
string Number;
bool IsTaken;
}
所以我的问题是:如何绑定ToggleButtons with List,以便我能够操作它们?
So my question is: how to bind those ToggleButtons with List, so that I will be able to operate on them?
这里的任何帮助非常感谢!
Any help here much appreciated!
推荐答案
创建一个 ItemsControl
并设置其 ItemTemplate
成为您的 ToggleButton
,绑定 ItemsSource
到你的座位收藏。从处理器中只需要一些自定义逻辑。
Create an ItemsControl
and set its ItemTemplate
to be your ToggleButton
, bind the ItemsSource
to your seat-collection. From there its just some custom logic in the handler.
为绑定对象中的按钮状态提供属性可能是有利的,那么您可以绑定 IsEnabled
和 IsChecked
。
It would probably be advantageous to provide properties for the button-state in your bound object as, then you can bind the IsEnabled
and IsChecked
to those.
这篇关于如何将ToggleButtons与列表绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!