c# - ContentControl 上的双向绑定(bind)

标签 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;
            }               
        }
    }


}

通过资源类选择数据模板:

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 绑定(bind)到键 TextBox (txtKey) 和 DataTemplate TextBox (txtTextIET1),但更改 txtTextIET1 上的值不会触发 KeyValuePair 属性上的 setter 。我找不到这种情况的任何示例,因此我们将不胜感激。

最佳答案

这对你有用吗<​​/p>

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

关于c# - ContentControl 上的双向绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2971267/

相关文章:

c# - 什么是程序集?

wpf - 带有绑定(bind)参数的 MarkupExtension

wpf - 数据绑定(bind)和锁定列表

c# - XDocument.Save() 时出现内存异常

c# - TwoWay 绑定(bind)一个实现 INotifyPropertyChanged 的​​类,没有任何反应

c# - WPF 使用 XAML 将 List<KeyValuePair<,>> 绑定(bind)到 DataGrid

wcf - Silverlight Combobox 数据绑定(bind)竞争条件

c# - 正确使用共享静态 'Random' 单例实例?

c# - 具有不同数据类型的主要方法

c# - 比较 SqlCommand 中的 DateTime