2009-07-14 61 views
5

私はviewmodelの文字列プロパティにバインドしているテキストボックスを持っています。文字列プロパティは、ビューモデル内で更新され、テキストボックス内のテキストをバインディングで表示します。WPFテキストボックスのバインディングと改行

問題は、文字列プロパティの特定の文字数の後に改行を挿入して、改行がテキストボックスコントロールに表示されるようにすることです。私はviewmodelの中の文字列プロパティ内ではなく、改行がテキストボックスの上に反映されていないのn \ rを\を追加​​してみました

(私はテキストボックス内でtrueに設定さAcceptsreturnプロパティを持っている)

誰でも助けることができます。

答えて

3

あなたの説明をする簡単なアプリを作成しただけで、それは私の仕事です。

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> 

ビューモデル:

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)); 
     } 
    } 
} 

ViewModelのインスタンスがWindowためDataContextとして設定されています。最後に、Button_Click()の実装は次のとおりです。

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

(私はビューが本当に直接のViewModelのTextプロパティを変更するべきではありませんが、これは単に迅速なサンプルアプリケーションであることを認識しています。)

これは、その結果TextBoxの最初の行に "Hello"という単語があり、2番目の行に "World"があります。

コードを投稿すると、このサンプルとは何が違うのか分かりますか?

+0

ありがとうAndy、私は私の最後に問題を考え出した。ご協力いただきありがとうございます。 – deepak

6

私の解決策は、HTMLでエンコードされた改行コード( )を使用することでした。

Line1&#10;Line2 

私は@Andyアプローチを好む直樹

0

から

Line1 
Line2 

のように見える、それは大規模でスクロール可能なテキストを小さな文字ではないに最適です。

ビューモデル

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"); 
    } 
} 

今、あなたはあなたのMVVMでTextWriteLine、TextWriteとTextClearを使用することができます。

関連する問題