gpt4 book ai didi

c# - 将所有系统颜色绑定(bind)到列表框

转载 作者:行者123 更新时间:2023-12-02 17:45:43 25 4
gpt4 key购买 nike

我想将所有 Windows.UI.Colors 绑定(bind)到通用 Windows 应用程序(适用于 Windows 10、Visual Studio 2015)中 XAML 页面中的 ListBox(ListView?)。

I found通过这种方式获取所有系统颜色:

Dictionary<string, Windows.UI.Color> Colors()
{
var _Colors = typeof(Windows.UI.Colors)
// using System.Reflection;
.GetRuntimeProperties()
.Select(c => new
{
Color = (Windows.UI.Color)c.GetValue(null),
Name = c.Name
});
return _Colors.ToDictionary(x => x.Name, x => x.Color);
}

我不知道如何将它绑定(bind)到ListBox

<ListBox ItemsSource="{x:Bind colors}" >
</ListBox>

理想情况下,列表项文本应为颜色名称,列表项背景应为颜色值。

最佳答案

@Romasz 答案的另一种方法:

Color() 方法更改为属性,并返回一个包含 SolidColorBrush 值的字典,而不是 Color,如下所示:

  public Dictionary<string, SolidColorBrush> Colors
{
get
{
var _Colors = typeof(Windows.UI.Colors)
// using System.Reflection;
.GetRuntimeProperties()
.Select(c => new
{
Color = new SolidColorBrush((Windows.UI.Color)c.GetValue(null)),
Name = c.Name
});

return _Colors.ToDictionary(x => x.Name, x => x.Color);
}
}

然后,在 XAML 中,将列表框更改为:

<ListBox ItemsSource="{x:Bind Colors}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Background="{Binding Value}">
<TextBlock Text="{Binding Key}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

关于c# - 将所有系统颜色绑定(bind)到列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33616251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com