C# WPF 文本框绑定和换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1124689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:46:27  来源:igfitidea点击:

WPF Textbox binding and line breaks

c#wpfbindingtextbox

提问by deepak

I have a textbox that i am binding to the viewmodel's string property. The string property is updated within the viewmodel and it displays the text within the textbox through binding.

我有一个文本框,我绑定到视图模型的字符串属性。string 属性在 viewmodel 中更新,它通过绑定在文本框中显示文本。

The issue is that i want to insert the line break after a certain number of characters in the string property and i want that the line break is shown over the textbox control.

问题是我想在字符串属性中的一定数量的字符之后插入换行符,并且我希望换行符显示在文本框控件上。

I tried appending \r\n inside the string property in viewmodel but the line break is not reflected over the textbox (i have Acceptsreturn property set to true inside the textbox)

我尝试在 viewmodel 的字符串属性中附加 \r\n 但换行符没有反映在文本框上(我在文本框中将 Acceptsreturn 属性设置为 true)

Can anybody help.

任何人都可以帮忙。

采纳答案by Andy

I just created a simple app that does what you describe, and it worked for me.

我刚刚创建了一个简单的应用程序,可以按照您的描述进行操作,并且对我有用。

XAML:

XAML:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <TextBox Grid.Row="0" AcceptsReturn="True" Height="50"
            Text="{Binding Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        <Button Grid.Row="1" Click="Button_Click">Button</Button>
    </Grid>
</Window>

ViewModel:

视图模型:

class ViewModel : INotifyPropertyChanged
{
    private string text = string.Empty;
    public string Text
    {
        get { return this.text; }
        set
        {
            this.text = value;
            this.OnPropertyChanged("Text");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propName)
    {
        var eh = this.PropertyChanged;
        if(null != eh)
        {
            eh(this, new PropertyChangedEventArgs(propName));
        }
    }
}

An instance of ViewModelis set as the DataContextfor the Window. Finally, the implementation of Button_Click()is:

的一个实例ViewModel被设置DataContext为 的Window。最后的实现Button_Click()是:

private void Button_Click(object sender, RoutedEventArgs e)
{
    this.model.Text = "Hello\r\nWorld";
}

(I realize that the view shouldn't really modify the ViewModel's Textproperty directly, but this is just a quick sample app.)

(我意识到视图不应该Text直接修改 ViewModel 的属性,但这只是一个快速示例应用程序。)

This results in the word "Hello" on the first line of the TextBox, and "World" is on the second line.

这导致 的第一行出现单词“Hello”,第二行出现TextBox“World”。

Maybe if you post your code we can see what is different from this sample?

也许如果您发布您的代码,我们可以看到与此示例有何不同?

回答by Thang.NQ

The solution for me was to use HTML encoded line feeds ( ).

我的解决方案是使用 HTML 编码的换行符 ( )。

Line1&#10;Line2

Looks like

好像

Line1
Line2

From Naoki

从直木

回答by Waleed A.K.

I Like @Andy Approach, it is perfect for small text not with large and scrollable text.

我喜欢@Andy Approach,它非常适合小文本,而不是大文本和可滚动文本。

View Model

查看模型

class ViewModel :INotifyPropertyChanged
{
    private StringBuilder _Text = new StringBuilder();
    public string Text
    {
        get { return _Text.ToString(); }
        set
        {
            _Text = new StringBuilder( value);
            OnPropertyChanged("Text");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propName)
    {
        var eh = this.PropertyChanged;
        if(null != eh)
        {
            eh(this,new PropertyChangedEventArgs(propName));
        }
    }
    private void TextWriteLine(string text,params object[] args)
    {
        _Text.AppendLine(string.Format(text,args));
        OnPropertyChanged("Text");
    }

    private void TextWrite(string text,params object[] args)
    {
        _Text.AppendFormat(text,args);
        OnPropertyChanged("Text");
    }

    private void TextClear()
    {
        _Text.Clear();
        OnPropertyChanged("Text");
    }
}

Now you can use TextWriteLine, TextWrite and TextClear in your MVVM.

现在您可以在 MVVM 中使用 TextWriteLine、TextWrite 和 TextClear。