views:

153

answers:

2

I'm creating a Key/Value pair editor and would like for the value to have a custom template based on the data type.

<TextBox x:Uid="txtKey" x:Name="txtKey" Grid.Column="1" Grid.Row="0" Text="{Binding ElementName=This, Path=KeyValuePair.Key}" VerticalAlignment="Top"/>
<ContentControl Grid.Column="1" Grid.Row="1"
           x:Uid="ContentControl1" x:Name="ContentControl1" 
           Content="{Binding ElementName=This, Path=KeyValuePair.Value}" 
           LostFocus="ContentControl1_LostFocus"  
           DataContextChanged="ContentControl1_DataContextChanged"   />

The codebehind for the host class looks like this:

public partial class KeyValueControl : ControlBase
{
    private System.Collections.DictionaryEntry _dictionaryEntry;
    private KeyValuePairObjectObject _KeyValuePair = new KeyValuePairObjectObject();
    private DataTemplate _editorDataTemplate;
    private Caelum.Libraries.Ui.Editors.Resources resources = new Editors.Resources();

    public DataTemplate EditorDataTemplate
    {
        get { return _editorDataTemplate; }
        set { _editorDataTemplate = value; SendPropertyChanged("EditorDataTemplate"); }
    }

    public KeyValuePairObjectObject KeyValuePair
    {
        get { return _KeyValuePair; }
        set { _KeyValuePair = value; SendPropertyChanged("KeyValuePair"); }
    }


    public KeyValueControl()
    {
        InitializeComponent();
        this.DataUpdated += new DataUpdatedHander(KeyValueControl_DataUpdated);
        DataContextChanged += new DependencyPropertyChangedEventHandler(KeyValueControl_DataContextChanged);
    }

    void KeyValueControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
    }

    public override void Save()
    {
        base.Save();
    }

    void KeyValueControl_DataUpdated(object sender, object data)
    {
        if (Data != null)
        {
            _dictionaryEntry = (System.Collections.DictionaryEntry)Data;
            KeyValuePair.Key = _dictionaryEntry.Key;
            KeyValuePair.Value = _dictionaryEntry.Value;

            if (KeyValuePair.Value != null)
            {
                EditorDataTemplate = resources.GetDataTemplate(_dictionaryEntry.Value.GetType());
                ContentControl1.ContentTemplate = EditorDataTemplate;
            }               
        }
    }


}

DataTemplates are chosen via the resources class:

public DataTemplate GetDataTemplate(Type type)
    {

        if (type == typeof(string))
        {
            return TextInlineEditorTemplate;
        }
        if (type == typeof(bool))
        {
            return BooleanInlineEditorTemplate;
        }

        return null;
    }

The DataTemplate that is displayed for a string is:

<DataTemplate x:Uid="TextInlineEditorTemplate" x:Key="TextInlineEditorTemplate"  >
    <Grid>
        <TextBox x:Uid="txtTextIET1" x:Name="txtTextIET1" Width="300" Text="{Binding Path=DataContext, Mode=TwoWay, RelativeSource={RelativeSource Self}, BindsDirectlyToSource=True, UpdateSourceTrigger=PropertyChanged}" />
    </Grid>
</DataTemplate>

The data binds OK to the key TextBox (txtKey) and DataTemplate TextBox (txtTextIET1), but changing the value on txtTextIET1 will not trigger the setter on the KeyValuePair property. I've not been able to find any examples of this scenario, so any help would be appreciated.

A: 

Didn't this work for you

<DataTemplate x:Uid="TextInlineEditorTemplate" x:Key="TextInlineEditorTemplate"  > 
    <Grid> 
        <TextBox x:Uid="txtTextIET1" x:Name="txtTextIET1" Width="300" Text="{Binding}" /> 
    </Grid> 
</DataTemplate>
Veer
A: 

No I received an error "Two-way binding requires Path or XPath."