tags:

views:

20

answers:

1

I have a datagrid in which one column would be title. I want the title to be able to be editted and the edit to be a comboBox that contains a list of titles based on the sex of the person the row represents. So say one would have "Mr" in the drop down for men and then the other would have "Ms.", "Mrs." and "Miss" for women. So I can't figure out a way to get this to work and have the item source change depending on the value of the bound object. Here's a snip of what (I have so far...it works fine for the comboBox's itemsource but the trigger doesn't update the source. I'm not sure if there is a better way to do this I've also looked at a template selector but I'm open to any suggestions.

    <my:DataGrid AutoGenerateColumns="False" Margin="30,20,130,77" Name="dataGrid1">
        <my:DataGrid.Columns>
            <my:DataGridTemplateColumn Header="Title" MinWidth="100">
                <my:DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding Path = Sex}" Value ="F">
                                <Setter Property="ComboBox.ItemsSource" Value="{StaticResource Titles2}" />
                            </DataTrigger>
                        </DataTemplate.Triggers>
                        <ComboBox ItemsSource="{StaticResource Titles}" 
                                  SelectedItem="{Binding Title}" />
                    </DataTemplate>
                </my:DataGridTemplateColumn.CellEditingTemplate>
                <my:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Title}" />
                    </DataTemplate>
                </my:DataGridTemplateColumn.CellTemplate>
            </my:DataGridTemplateColumn>

Thanks for any help!!

+1  A: 

The problem is that the trigger is setting the property on the ContentPresenter, not on the ComboBox. When you write Property="ComboBox.ItemsSource", you are qualifying the dependency property you want to set, but it is still set on the ContentPresenter and is not inherited by the ComboBox. You can set a property on the ComboBox by giving it a Name attribute and setting TargetName on the Setter:

<DataTemplate>
    <ComboBox ItemsSource="{StaticResource Titles}" 
              Name="myComboBox"
              SelectedItem="{Binding Title}" />
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Path = Sex}" Value ="F">
            <Setter TargetName="myComboBox" Property="ItemsSource"
                    Value="{StaticResource Titles2}" />
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>
Quartermeister
Thank you very much! Works perfectly!
Mark