从Colors listBox中获取所选颜色

时间:2015-07-08 21:13:05

标签: c# wpf xaml listbox

我试图在ListBox中显示所有颜色。使用example

这里是代码

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    x:Class="WpfApplication1.MainWindow"
    x:Name="Window"
    Title="All Colors"
    Width="640" Height="480" >

    <Window.Resources>
        <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>
    </Window.Resources>

    <ListBox ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}"
        ScrollViewer.HorizontalScrollBarVisibility="Disabled"
        ScrollViewer.VerticalScrollBarVisibility="Auto" >
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <Rectangle Fill="{Binding Path=Name}" Stroke="Black" Margin="4" StrokeThickness="1" Height="50" Width="81"/>
                <Label Content="{Binding Path=Name}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
    </ListBox>
</Window>

我希望从ListBox selectedItem得到System.Windows.Media.Color的所选颜色。但是当我将所选项目转换为Color

时,它总是返回null

enter image description here

2 个答案:

答案 0 :(得分:2)

那是因为listbox.SelectedItem的类型为System.Reflection.PropertyInfo。它所引用的是保持所选颜色的System.Windows.Media.Color类的静态属性。

System.Reflection.PropertyInfo prop = (System.Reflection.PropertyInfo)listbox.SelectedItem;
Color color = (Color)prop.GetValue(null, null);
string colorName = prop.Name;

color即被选中的System.Windows.Media.ColorcolorName是该属性的名称(例如AzureAquamarine)。

答案 1 :(得分:0)

查看您的XAML,您还没有给出您尝试从名称中获取值的列表框。因此,我不确定为什么获取所选颜色的代码不会引发错误。

当我想从某种形式的ListView中获取值时,我会做两件事。

首先在视图模型中添加一个属性,或者在后面添加代码以保存所选颜色。

Color selectedColor {get; set;}

然后稍微更改ListBox XAML的开头,以便将所选值绑定到此属性。所以你的XAML会这样开始:

<ListBox ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}"
    SelectedValue={Binding selectedColor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged"}
    ScrollViewer.HorizontalScrollBarVisibility="Disabled"
    ScrollViewer.VerticalScrollBarVisibility="Auto" >

然后你应该能够从&#39; selectedColor&#39;中读取所选的值。属性。

相关问题