Kassem
Kassem

Reputation: 8276

Troubles with custom controls and binding

I'm currently building a bunch of common use controls which basically just extend the default Silverlight controls by adding a label and stuff like that... But I'm having a hard time getting some bindings to work... Below are some examples of what currently doesn't work:

UPDATE:

Here's some more context to help understand the problem:

public class ComboboxField : Field
{

    public string SelectedValuePath
    {
        get { return (string)this.GetValue(SelectedValuePathProperty); }
        set { this.SetValue(SelectedValuePathProperty, value); }
    }
    public static readonly DependencyProperty SelectedValuePathProperty =
    DependencyProperty.Register("SelectedValuePath", typeof(string), typeof(ComboboxField), new PropertyMetadata(string.Empty));


    public string DisplayMemberPath
    {
        get { return (string)this.GetValue(DisplayMemberPathProperty); }
        set { this.SetValue(DisplayMemberPathProperty, value); }
    }
    public static readonly DependencyProperty DisplayMemberPathProperty =
      DependencyProperty.Register("DisplayMemberPath", typeof(string), typeof(ComboboxField), new PropertyMetadata(string.Empty, null));



    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)this.GetValue(ItemsSourceProperty); }
        set { this.SetValue(ItemsSourceProperty, value); }
    }

    public static readonly DependencyProperty ItemsSourceProperty =
    DependencyProperty.Register("ItemsSource", typeof(Object), typeof(ComboboxField), new PropertyMetadata(new List<object>()));



    public object SelectedValue 
    {
        get { return (object)GetValue(SelectedValueProperty); }
        set { SetValue(SelectedValueProperty, value); }
    }

    public static readonly DependencyProperty SelectedValueProperty =
        DependencyProperty.Register("SelectedValue", typeof(object), typeof(ComboboxField), new PropertyMetadata(null, (s, e) => { s.SetValue(Field.ValueProperty, SelectedValueProperty);}));

    #region Ctors
    public ComboboxField(FieldDescription fieldDescription, Form parentForm) : base(fieldDescription, parentForm)
    {
        this.Template = Application.Current.Resources["ComboBoxDefaultTemplate"] as ControlTemplate;
    }

    public ComboboxField() : base(new FieldDescription(Guid.NewGuid(), "ComboboxField1", FieldType.Collection), null)
    {
        this.Template = Application.Current.Resources["ComboBoxDefaultTemplate"] as ControlTemplate;
    }
    #endregion

}

The Control Template: (removed some of the irrelevant stuff)

<ControlTemplate x:Name="ComboBoxDefaultTemplate" TargetType="my:ComboboxField">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="{TemplateBinding LabelColumnWidth}" />
                    <ColumnDefinition Width="{TemplateBinding FieldColumnWidth}" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Row="0" Grid.Column="0" Style="{TemplateBinding TitleStyle}" Text="{TemplateBinding Title}"></TextBlock>
                <TextBlock Text="*" Grid.Row="0" Grid.Column="0" Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Required, Converter={StaticResource boolToVisibility}}" Width="5" />
                <TextBlock Grid.Row="1" Grid.Column="0" Style="{TemplateBinding SummaryStyle}" Text="{TemplateBinding Summary}" Grid.ColumnSpan="2"></TextBlock>
<ComboBox Style="{TemplateBinding FieldStyle}" Grid.Row="0" Grid.Column="1"
                          ItemsSource="{TemplateBinding ItemsSource}" 
                          SelectedValue="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectedValue, Mode=TwoWay}" 
                          SelectedValuePath="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectedValuePath, Mode=TwoWay}" 
                          DisplayMemberPath="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DisplayMemberPath, Mode=TwoWay}"></ComboBox>
            </Grid>
        </ControlTemplate>

The way I'm trying to use it:

<cc:ComboboxField Grid.Row="10" TitleStyle="{StaticResource labelStyle}" 
                                  Title="Countries" ItemsSource="{Binding Countries}" 
                                  SelectedValuePath="CountryId" DisplayMemberPath="CountryId" 
                                  SelectedValue="{Binding Path=SelectedCountry, Mode=TwoWay}" />

What am I doing wrong here?

Upvotes: 0

Views: 559

Answers (1)

Luke Woodward
Luke Woodward

Reputation: 65054

I can see immediately a couple of problems with your dependency properties.

Firstly, I have a Golden Rule for working with dependency properties, which your code violates:

The CLR property getter must contain only a call to GetValue and the setter must contain only a call to SetValue.

For example, the SelectedValuePath property should look like the following:

public string SelectedValuePath
{
    get { return (string)this.GetValue(SelectedValuePathProperty); }
    set { this.SetValue(SelectedValuePathProperty, value); }
}

Silverlight doesn't call your property if it wants to change the value of the dependency property using binding, animation or styles. If you drop breakpoints on any of the setters, you'll find they won't get hit as often as you might expect.

You can bind together two dependency properties without the class on either end needing to implement INotifyPropertyChanged. I assume your NotifyPropertyChanged method is firing the PropertyChanged event, which you should be able to remove.

Also, if you want the Value property set to the value of the SelectedValue property every time the latter changes, you'll need to add a PropertyChangedCallback to the SelectedValue dependency property.

Secondly, the dependency property registrations for SelectedValuePathProperty, DisplayMemberPathProperty and ItemsSourceProperty are wrong. They all specify their owner class as Field when they are owned by the ComboboxField class instead.

Finally, you say that you are

having a hard time getting some bindings to work

You're not telling us exactly what your problem is. What are you expecting the code to do and what is it actually doing? Given that I don't have your Form, Field and FieldDescription classes nor your FieldType enum, I can't run your code, so I can't really help you any more.

Upvotes: 1

Related Questions