2010-06-04 112 views
2

我正在创建一个键/值对编辑器,并希望该值具有基于数据类型的自定义模板。ContentControl上的双向绑定

<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" /> 

主机类代码隐藏看起来像这样:

public DataTemplate GetDataTemplate(Type type) 
    { 

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

     return null; 
    } 

所显示的字符串的DataTemplate中是:

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经由资源类选择

<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> 

数据绑定OK键TextBox(txtKey)和DataTemplate TextBox(txtTextIET1),但更改txtTextIET1上的值不会触发KeyValuePair属性上的setter。我一直无法找到这种情况的任何例子,所以任何帮助,将不胜感激。

回答

0

没有这个工作对你

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

随着该绑定会得到读取错误“双向绑定需要路径或XPath。”您可以通过将绑定从{绑定}更改为{绑定}来解决该特定问题,但它似乎仍不能让TwoWay绑定通过ContentControl工作。 – jpierson 2013-02-19 16:49:33