从颜色列表框中获取选定的颜色
Get the selected color from Colors 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
时,它总是 return null
查看您的 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" >
那么您应该能够从 'selectedColor' 属性.
中读取选定的值
那是因为 listbox.SelectedItem
的类型是 System.Reflection.PropertyInfo
。它引用的是 System.Windows.Media.Color
class 的静态 属性,它保存了选定的颜色。
System.Reflection.PropertyInfo prop = (System.Reflection.PropertyInfo)listbox.SelectedItem;
Color color = (Color)prop.GetValue(null, null);
string colorName = prop.Name;
color
就是被选中的System.Windows.Media.Color
,colorName
就是属性的名称(比如Azure
或者Aquamarine
).
我试图在 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
查看您的 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" >
那么您应该能够从 'selectedColor' 属性.
中读取选定的值那是因为 listbox.SelectedItem
的类型是 System.Reflection.PropertyInfo
。它引用的是 System.Windows.Media.Color
class 的静态 属性,它保存了选定的颜色。
System.Reflection.PropertyInfo prop = (System.Reflection.PropertyInfo)listbox.SelectedItem;
Color color = (Color)prop.GetValue(null, null);
string colorName = prop.Name;
color
就是被选中的System.Windows.Media.Color
,colorName
就是属性的名称(比如Azure
或者Aquamarine
).