【问题标题】:Read and preset a combobox of color properties读取并预设颜色属性的组合框
【发布时间】:2016-08-08 09:28:22
【问题描述】:

从教程中我有以下组合框:

<ComboBox Name="comboBox_warnColor" Margin="5,0,5,0" SelectionChanged="comboBox_warnColor_SelectionChanged">
        <ComboBox.ItemTemplate>
                <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                                <Rectangle Fill="{Binding Name}" Width="12" Height="12" Margin="0,2,5,2" />
                                <TextBlock Name="PART_ColorName" Text="{Binding Name}" />
                        </StackPanel>
                </DataTemplate>
        </ComboBox.ItemTemplate>
</ComboBox>

我这样填充它:

comboBox_warnColor.ItemsSource = typeof(Colors).GetProperties();

我想将所选项目保存在文本文件中,以便稍后再次将其加载到组合框。

在这种情况下使用它可以节省什么价值? 或者 什么是默认定义颜色的好 ID 值?

SelectedIndex 除了我找不到。

【问题讨论】:

  • 您查看过 SelectedItem 吗?它返回与您的 ItemSource 集合中相同的类型。您可以从中提取 Name 或任何您想要的属性。
  • 这正是重点。当然,我可以使用comboBox_warnColor.SelectedItem.ToString().Split(' ')[1] 之类的方法来提取它,但这似乎不是很好的风格。
  • 所以您的实际问题是,对于默认定义的颜色,什么是好的 ID 值?
  • 没错。我添加了你的措辞作为我的问题。
  • 您是否已经尝试将&lt;ComboBox SelectedValuePath="Name" SelectedValue="{Binding Path=SelectedName}" ...SelectedName 作为DataContext 中的一个属性?

标签: c# wpf combobox


【解决方案1】:

您可以使用SelectedValuePathSelectedValue,如下所示:

<ComboBox SelectedValuePath="Name" SelectedValue="{Binding SelectedName}" Name="comboBox_warnColor" Margin="5,0,5,0">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Rectangle Fill="{Binding Name}" Width="12" Height="12" Margin="0,2,5,2" />
                <TextBlock Name="PART_ColorName" Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

在后面的代码中:

public class VeryUnrefinedViewmodel
{
    public string SelectedName { get; set; }
}

// ...

VeryUnrefinedViewmodel _DataContextInstance = new VeryUnrefinedViewmodel();
// your initialization code
comboBox_warnColor.ItemsSource = typeof(Colors).GetProperties();
comboBox_warnColor.DataContext = _DataContextInstance;
// _DataContextInstance.SelectedName will contain the name of the selected color

您可以通过为SelectedName 设置初始值以编程方式预先选择颜色。如果要动态改变选择,需要为viewmodel实现INotifyPropertyChanged

 VeryUnrefinedViewmodel _DataContextInstance = new VeryUnrefinedViewmodel() { SelectedName = "Yellow" };

【讨论】:

  • 它有效,但我认为它有点不方便。另一方面,这也可能适用于我的程序。
  • @MarcoFrost 我想这很不方便,因为您的应用程序不在 MVVM 设计中,所以您有不同的样板代码。顺便说一句(我之前什至没有想到这一点):您可以在后面的代码中访问comboBox_warnColor.SelectedValue,而不是使用数据上下文和视图模型......也许这更符合您的其余代码。跨度>
  • 嗯,这太容易了。我不知道组合框会接受一个字符串作为选择项目的值。毕竟那个组合框中有两个子控件。
  • @MarcoFrost 是 SelectedValuePath 的绝妙之处——只要每件物品都具有某种独特的价值属性,它就会像魅力一样发挥作用。
  • @MarcoFrost 您可以将SelectedValue 视为getter 的SelectedItem.&lt;SelectedValuePath&gt; 和setter 的SelectedItem = ItemsSource.FirstOrDefault(x =&gt; x.&lt;SelectedValuePath&gt; == value)。当然,最后还是要复杂一些。
【解决方案2】:

RGBA 可以在唯一模式下确定颜色,因此您可能希望将其存储为 ID。 您可以使用question 将颜色与 RGBA 进行转换。

【讨论】:

  • 我已经想到了。问题是,使用上述填充 ComboBox 的方式,我无法通过 rgba 值直接选择项目。
  • 您可以在绑定中使用转换器将 RGBA 转换为颜色。
猜你喜欢
  • 2022-01-15
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多