2

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

<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 属性上的设置器。我无法找到这种情况的任何示例,因此将不胜感激。

4

1 回答 1

0

这不是对你有用吗

<DataTemplate x:Uid="TextInlineEditorTemplate" x:Key="TextInlineEditorTemplate"  > 
    <Grid> 
        <TextBox x:Uid="txtTextIET1" x:Name="txtTextIET1" Width="300" Text="{Binding}" /> 
    </Grid> 
</DataTemplate>
于 2010-06-04T08:45:44.190 回答