How to Databind a Combobox

Jan 30, 2010 at 2:10 AM

Hi All!

I created a new property named "Character" to the person objcect. And I added a new combo-box to the "Add a New Family Member" screen.

How do I bind the combobox to the new "Character' property? I know I need to edit the Details/xaml. But what is the syntax pls?

Thanks!

Developer
Jan 30, 2010 at 11:54 AM
Edited Jan 30, 2010 at 12:04 PM

Is the "Character" property a string or enum or somthing else?

I think that it is easy to bind the possible values of Character e.g. "Character1", "Character2" etc to the combobox.  However, the New Person dialog only creates the person after you have clicked add.  So you'd have to read the value from the combobox and set it manully in the C# code as it's not possible to bind that value.  You can however use data binding in the Edit Details panel once the person is created so changing the combo box value will change the person's Character property.

Can't guarentee the code below will work first time, but it should get you on the way to a solution.

Details.xaml

<UserControl.Resources>
    <ResourceDictionary>
    <!-- Data provider for binding to the character types enum values -->
    <ObjectDataProvider x:Key="Characters" ObjectType="{x:Type sys:Enum}" MethodName="GetValues">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:CharacterType" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
    <UserControl.Resources>
<ResourceDictionary>

<!-- Combo box for add new person screen -->
<ComboBox ItemsSource="{Binding Source={StaticResource Characters}}" x:Name="NewCharacterComboBox" />

<!-- Combo box for details edit screen -->
<ComboBox ItemsSource="{Binding Source={StaticResource Characters}}" x:Name="DetailsEditCharacterComboBox" SelectedValue="{Binding Path=Character, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

A new file called CharcterType.cs

namespace Microsoft.FamilyShow
{
    public enum CharacterType
    {
        Character1,
        Character2
    }
}

Details.xaml.cs

// In the add new person code read the selected character
if (NewCharacterComboBox.SelectedValue!=null)
{
    person.Character = (CharacterType)NewCharacterComboBox.SelectedValue;
}

Person.cs

private CharacterType character;

public CharacterType Character
        {
            get
            {
                return character;
            }
            
            set
            {
                if (character != value)
                {
                    character = value;
                    OnPropertyChanged("Character");
                }
            }
        }
Feb 2, 2010 at 12:25 AM

This code works:

<font size="2" color="#a31515"><font size="2" color="#a31515">

 

</font></font><font size="2" color="#a31515">

 

</font>

<Label Content="_Character" Target="{Binding ElementName=CharacterComboBox2, Mode=OneWay}" Foreground="{DynamicResource InputFontColor}" />

<ComboBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource CharacterValuesEnumDS}}" x:Name="CharacterComboBox2" Width="135" Grid.Row="1" HorizontalAlignment="Left" Margin="5,0,0,0" ItemTemplate="{DynamicResource CharacterDataTemplate}" Style="{DynamicResource CharacterComboBox}" Text="{Binding Path=Character, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Thanks!