我对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>();
您可能首先需要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>