Custom control binding issue in silverlight
I'm trying to set up the binding of a custom control in Silverlight 3 and I'm having weird problems with it.
My xaml for the custom control:
<UserControl x:Class="StronicoMain.GenericSmallIcon"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300" >
<Canvas x:Name="canGSI">
</Canvas>
</UserControl>
The codename for the custom control is
private string _EntityTypeID;
public string EntityTypeID
{
get
{
return _EntityTypeID;
}
set
{
_EntityTypeID = value;
}
}
public GenericSmallIcon()
{
InitializeComponent();
Loaded += new RoutedEventHandler(Page_Loaded);
}
public void Page_Loaded(object sender, RoutedEventArgs e)
{
icoMale icoMale = new icoMale();
icoFemale icoFem = new icoFemale();
if (EntityTypeID == null)
{
canGSI.Children.Add(icoMale);
}
else if (EntityTypeID == "1")
{
canGSI.Children.Add(icoMale);
}
else if (EntityTypeID == "2")
{
canGSI.Children.Add(icoFem);
}
else
{
canGSI.Children.Add(icoMale);
}
}
I call it from the DataGridSelection adapter (taken from the Microsoft Toolkit sample example for AutoCompleteBox-Datagrid version) - the relevant part looks like this:
<Stron:DataGridSelectionAdapter x:Name="SelectionAdapter" AutoGenerateColumns="False" IsReadOnly="False">
<Stron:DataGridSelectionAdapter.Columns>
<data:DataGridTemplateColumn>
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate><Stron:GenericSmallIcon EntityTypeID="{Binding EntityTypeID}"></Stron:GenericSmallIcon></DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
<data:DataGridTextColumn Header="Contact Name" FontWeight="Bold" Foreground="#CC000000" Binding="{Binding EntityName}" />
<data:DataGridTextColumn Header="Tags" Binding="{Binding EntityTags}" />
</Stron:DataGridSelectionAdapter.Columns>
</Stron:DataGridSelectionAdapter>
I run the code and I get the error "ManagedRuntimeError # 4004" - if I try to use a custom control when manually configuring the data binding, it works fine, if I try to rely on the values ββthat are the data binding I get the error. How do I create a custom data binding event in a custom control? I think it is a problem that the page is being loaded before the values ββare passed to it.
Thanks everyone.
~ Steve
Update, here is a working, modified code as per the accepted answer
public static readonly DependencyProperty EntityTypeIDProperty = DependencyProperty.Register("EntityTypeID", typeof(string), typeof(GenericSmallIcon), new PropertyMetadata(new PropertyChangedCallback(GenericSmallIcon.OnEntityTypeIDPropertyChanged)));
public string EntityTypeID
{
get { return (string)GetValue(EntityTypeIDProperty); }
set { SetValue(EntityTypeIDProperty, value); }
}
private static void OnEntityTypeIDPropertyChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
GenericSmallIcon control = d as GenericSmallIcon;
string b = (string)e.NewValue;
}
source to share
This is not true:
<Stron:GenericSmallIcon EntityTypeID="{Binding EntityTypeID}"/>
The problem is that you cannot bind to a property that is not a dependency property. See the MSDN article on converting your POCO property to a Dependency property.
-Mark
source to share