想要用字典中填充的ToggleButtons填充

问题描述 投票:0回答:1

我对MVVM有点陌生,我必须说这并不容易...正如主题所说,要使用从Dictionary中填充的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个回答
0
投票

您可能首先需要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>
© www.soinside.com 2019 - 2024. All rights reserved.