c# - Get the selected color from Colors listBox -
i'm trying shows colors in listbox
. using example
and here's code
<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>
i want selected color listbox selecteditem
system.windows.media.color
. return null when cast selected item color
that's because listbox.selecteditem
of type system.reflection.propertyinfo
. it's referencing static property of system.windows.media.color
class holds selected 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.color
selected, , colorname
name of property (such azure
or aquamarine
).
Comments
Post a Comment