想要用从字典填充的 ToggleButtons 填充 <StackPanel>

Want to fill a <StackPanel> with ToggleButtons populated from a Dictionary

我对 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>();

您可能首先需要 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>