将字符串绑定到富文本框 [英] Binding a String to a richtextbox

查看:31
本文介绍了将字符串绑定到富文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个填充有notes"的数据网格,当点击一个注释时,我希望富文本框显示 note.comments.但是绑定不起作用.

I have a datagrid populated with "notes" and when a note is clicked I want the richtextbox to show the note.comments. But the Bindings isn't working.

public NoteDTO SelectedNote {get; set;}
public string stringNotes {get; set;}

public void OpenNote()
{
    stringNotes = SelectedNote.Comments;
}



<DataGrid x:Name="NoteGrid" cal:Message.Attach="[Event MouseDoubleClick] = [Action OpenNote()]" ItemsSource="{Binding Notes}" SelectedItem="{Binding SelectedNote}"


<toolkit:RichTextBox Text="{Binding stringNotes, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

如果我可以得到帮助,请.

If I may get help please.

推荐答案

主要问题是您绑定到一个没有更改通知概念的属性;你没有实现 INotifyPropertyChanged.话虽如此,为什么不直接将 RichTextBox 绑定到 NoteDTO 的属性上:

The main problem is that you're binding to a property that has no concept of change notifications; you're not implementing INotifyPropertyChanged. That being said, why not just bind the RichTextBox directly to the property off of NoteDTO:

<toolkit:RichTextBox Text="{Binding SelectedNote.Comments, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

另一种选择是手动复制SelectedNotestringNotes 之间的注释,然后实现INotifyPropertyChanged,但这并不理想,除非您想要在将它们传播到 NoteDTO 对象之前拥有一个中间属性.

The other option is to manually copy the comments between SelectedNote and stringNotes, then implement INotifyPropertyChanged, but this isn't ideal unless you want to have an intermediate property before propagating them to the NoteDTO object.

我注意到您的 SelectedNote 属性永远不会通知 UI 它已更改,这将阻止绑定工作.请尝试以下操作:

I noticed that your SelectedNote property will never notify the UI that it has changed, which will prevent bindings from working. Try something like the following:

public class MyClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if(this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    private string selectedNote;
    public string SelectedNote
    {
        get { return this.selectedNote; }
        set
        {
            if (this.selectedNote == value)
                return;

            this.selectedNote = value;
            this.OnPropertyChanged("SelectedNote");
        }
    }
}

这篇关于将字符串绑定到富文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆