c# - DependencyProperty for ItemsSource -
i have xaml like:
<usercontrol x:class="book.customcontrols.headeredcombobox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" datacontext="{binding relativesource={relativesource self}}" width="200" height="50"> <grid> <grid.rowdefinitions> <rowdefinition height="auto"/> <rowdefinition/> </grid.rowdefinitions> <textblock grid.row="0" margin="2,2" verticalalignment="center" text="{binding header}" fontweight="{binding headerfontweight}"/> <combobox grid.row="1" margin="2,2" verticalalignment="center" itemssource="{binding itemssource, updatesourcetrigger=propertychanged}" selecteditem="{binding selecteditem, mode=twoway, updatesourcetrigger=propertychanged}"/> </grid> </usercontrol>
the cs-file therefor is:
public partial class headeredcombobox : usercontrol { public headeredcombobox() { this.initializecomponent(); this.headerfontweight = fontweights.normal; } public static readonly dependencyproperty headerproperty = dependencyproperty.register("header", typeof(string), typeof(headeredcombobox)); public static readonly dependencyproperty headerfontweightproperty = dependencyproperty.register("headerfontweight", typeof(fontweight), typeof(headeredcombobox)); public static readonly dependencyproperty itemssourceproperty = dependencyproperty.register("itemssource", typeof(ienumerable), typeof(headeredcombobox)); public static readonly dependencyproperty selecteditemproperty = dependencyproperty.register("selecteditem", typeof(object), typeof(headeredcombobox)); public string header { { return (string)getvalue(headerproperty); } set { setvalue(headerproperty, value); } } public fontweight headerfontweight { { return (fontweight)getvalue(headerfontweightproperty); } set { setvalue(headerfontweightproperty, value); } } public ienumerable itemssource { { return (ienumerable)getvalue(itemssourceproperty); } set { setvalue(itemssourceproperty, value); } } public object selecteditem { { return getvalue(selecteditemproperty); } set { setvalue(selecteditemproperty, value); } } }
i tried use usercontrol in window with
<customcontrols:headeredcombobox header="category" itemssource="{binding categories}"/>
categories observablecollection<string>
.
unfortunately don't see items in combobox. @ moment have no idea, i'm doing wrong. ideas?
system.windows.data error: 40 : bindingexpression path error: 'categories' property not found on 'object' ''headeredcombobox' (name='')'. bindingexpression:path=categories; dataitem='headeredcombobox' (name=''); target element 'headeredcombobox' (name=''); target property 'itemssource' (type 'ienumerable')
your datacontext set headeredcombobox , there system looking categories. property header worked since put text without binding. possible solution
<window x:class="wpfapplication5.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525" xmlns:local="clr-namespace:wpfapplication5" name="window"> <grid> <local:headeredcombobox header="category" itemssource="{binding elementname=window, path=datacontext.categories}"/> </grid>
Comments
Post a Comment