ContentControl上的双向绑定

时间:2010-06-04 03:11:37

标签: c# wpf data-binding 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 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:

public DataTemplate GetDataTemplate(Type type)
    {

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

        return null;
    }

为字符串显示的DataTemplate是:

<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。我无法找到任何这种情况的例子,所以任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

这不适合你吗

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