【问题标题】:How can I list colors in WPF with XAML?如何使用 XAML 在 WPF 中列出颜色?
【发布时间】:2009-02-18 20:27:23
【问题描述】:

如何获取我可以在 Visual Studio 设计器中选择的所有颜色的列表(即 System.Windows.Media.Colors,但这不是一个集合)并使用 WPF 和 XAML 标记将它们放入我自己的 ComboBox 中?

【问题讨论】:

  • 与 CasperOne 的建议类似,这是一种在 XAML 中完成所有操作的方法,可以在 WrapPanel 中很好地显示所有内容。 stuff.seans.com/2011/02/14/…

标签: c# wpf xaml


【解决方案1】:

这是纯 XAML 解决方案。

在你的资源部分,你会使用这个:

<!-- Make sure this namespace is declared so that it's in scope below -->
.. xmlns:sys="clr-namespace:System;assembly=mscorlib" ..

<ObjectDataProvider MethodName="GetType" 
    ObjectType="{x:Type sys:Type}" x:Key="colorsTypeOdp">
    <ObjectDataProvider.MethodParameters>
        <sys:String>System.Windows.Media.Colors, PresentationCore,
            Version=3.0.0.0, Culture=neutral, 
            PublicKeyToken=31bf3856ad364e35</sys:String>
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ObjectDataProvider ObjectInstance="{StaticResource colorsTypeOdp}"  
    MethodName="GetProperties" x:Key="colorPropertiesOdp">
</ObjectDataProvider>

或者,如CodeNaked points out,可以缩减为一个标签:

<ObjectDataProvider 
    ObjectInstance="{x:Type Colors}" 
    MethodName="GetProperties" 
    x:Key="colorPropertiesOdp" />

然后组合框将如下所示:

<ComboBox Name="comboBox1" 
    ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}"
    DisplayMemberPath="Name"
    SelectedValuePath="Name" />

【讨论】:

  • @casperOne - 您的解决方案可以浓缩为一行:&lt;ObjectDataProvider ObjectInstance="{x:Type Colors}" MethodName="GetProperties" x:Key="colorPropertiesOdp" /&gt;。这个答案相当古老,所以当时它可能不起作用。但我使用 .NET 3.0、3.5 和 4.0 项目进行了测试,并且每次都有效。
  • @casperOne 如果我想做SelectedItemBinding 值必须是什么类型?
【解决方案2】:

这是一个很棒的 ItemTemplate,可用于使用 casperOne's code 的组合框:

<ComboBox Name="cboColors"
          ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}"
          SelectedValuePath="Name">
     <ComboBox.ItemTemplate>
        <DataTemplate>
           <StackPanel Orientation="Horizontal" Height="18" Margin="0,0,0,2">
              <Border BorderThickness="1" CornerRadius="2" 
                  BorderBrush="Black" Width="50" VerticalAlignment="Stretch"
                  Background="{Binding Name}"/>
              <TextBlock Text="{Binding Name}" Margin="8,0,0,0"/>
           </StackPanel>
         </DataTemplate>
     </ComboBox.ItemTemplate>
</ComboBox>

【讨论】:

    【解决方案3】:

    这是我在过去的 ASP.net 应用中所做的:

    // populate colors drop down (will work with other kinds of list controls)
    Type colors = typeof(System.Drawing.Color);
    PropertyInfo[] colorInfo = colors.GetProperties(BindingFlags.Public |
        BindingFlags.Static);
    foreach ( PropertyInfo info in colorInfo)
    {
        ddlColor.Items.Add(info.Name);                
    }
    
    // Get the selected color
    System.Drawing.Color selectedColor = 
        System.Drawing.Color.FromName(ddlColor.SelectedValue);
    

    【讨论】:

      【解决方案4】:

      以下是如何在代码中使用反射来实现。以下会将所有预定义的 WPF 颜色名称转储到输出:

      using System.Reflection;
      
      void ListAllColors()
      {
          Type colorsType = typeof(System.Windows.Media.Colors);
          PropertyInfo[] colorsTypePropertyInfos = colorsType.GetProperties(BindingFlags.Public | BindingFlags.Static);
      
          foreach (PropertyInfo colorsTypePropertyInfo in colorsTypePropertyInfos)
              Debug.WriteLine(colorsTypePropertyInfo.Name);
      }
      

      要将它们放在组合框中,您只需将最后一行更改为:

      _comboBox.Items.Add(colorsTypePropertyInfo.Name); 
      

      【讨论】:

        猜你喜欢
        • 2011-06-17
        • 2010-12-30
        • 2023-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-14
        • 1970-01-01
        • 2016-08-07
        相关资源
        最近更新 更多