【问题标题】:Want to fill a <StackPanel> with ToggleButtons populated from a Dictionary想要用从 Dictionary 填充的 ToggleButtons 填充 <StackPanel>
【发布时间】:2020-03-23 20:03:16
【问题描述】:

我对 MVVM 有点陌生,我必须说这并不容易...... 正如主题所说,要使用从字典中填充的 ToggleButtons 填充 StackPanel。 有人可以让我的方向正确吗?

你好,Fonzie

 public class Soort
    {
        public int ID;            
        public Boolean Pressed;
        public string shortTitle;
        public string Title;
        public SolidColorBrush BorderColor;            
        public SolidColorBrush BackgroundColor;
        public int DefaultTime;           
    }

    public static Dictionary<int, Soort> dSoorten = new Dictionary<int, Soort>();

【问题讨论】:

标签: c# wpf mvvm binding togglebutton


【解决方案1】:

您可能首先需要ViewModel

public class Soort
{
    public int ID;            
    public Boolean Pressed {get;set;} //Must be read/write a property to enable two way binding
    public string shortTitle;
    public string Title {get;}
    public SolidColorBrush BorderColor;            
    public SolidColorBrush BackgroundColor;
    public int DefaultTime;           
}

public class SoortsViewModel
{
     public Dictionary<int, Soort> Soorts {get;}
}

然后将其绑定到ItemsControl 视图:

<ItemsControl Source="{Binding Soorts.Values}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ToggleButton Checked="{Binding IsPressed, Mode=TwoWay}" Content="{Binding Title}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

【讨论】:

  • 谢谢你,Andri,我还花了一天的时间去工作,但那是因为我没有按照你的例子做对 xD
猜你喜欢
  • 2010-11-21
  • 2011-07-11
  • 2013-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-04
  • 1970-01-01
相关资源
最近更新 更多