2016-11-02 28 views
0

ObservableCollectionの "struct-like"アイテムからTextChangedイベントを持つTextBoxへの一方向バインディングを実現したいと考えています。このアイデアは、ItemのCommentsフィールドがTextBoxに蓄積すると、TextBoxが自動的にスクロールして最後の行が常に表示されるようになっているということです。コレクションはListViewにバインドされていますが、TextBoxにバインドされた読み取り専用です。私は、ResultViewModelに別のメソッドを追加するのではなく、XAMLでそれを行うことを好みます。これを行うにはどうしたらいいですか? TIAバインディングObservableCollection <Item> To TextBox(UWP)

// ViewModel 

public class Item 
    {   
     public string First { get; set; } 

     public string Last { get; set; } 

     public string Comments { get; set; } 
    } 

public class ResultViewModel 
    { 
     private ObservableCollection<Item> items = new ObservableCollection<Item>();           

    public ObservableCollection<Item> Items { get { return items; } } 

     // member functions 
    } 

public ResultViewModel ViewModel { get; set; } 

// What I have 

      <ListView x:Name="myListView" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"      
        ItemsSource="{x:Bind ViewModel.Items}"> 
       <ListView.ItemTemplate> 
        <DataTemplate x:DataType="local:Item"> 
         <StackPanel> 
          <TextBox Text="{x:Bind First}"/> 
          <TextBlock Text="{x:Bind Last}"/> 
          <TextBlock Text="{x:Bind Comments}"/> 
         </StackPanel> 
        </DataTemplate> 
       </ListView.ItemTemplate> 
      </ListView> 

// What I want 

<TextBox Text="{x:Bind Comments}"/> 
+0

あなたが望むものを正確に理解するのは少し難しいです。ユーザーが変更できないTextBoxを持っていますが、Commentsプロパティの値が変更され、バインドされたTextBoxを下にスクロールしたい場合は、 –

+0

それは正しいですが、TextBoxはユーザーが変更することはできませんが、Commentsプロパティが累積してTextBoxを超えているので、TextBoxを最後までスクロールして最後の行を表示します。これは、TextBoxにバインドしたいという背後にある動機です。 TextBoxがListViewの内部に埋め込まれているこの場合、スクロール・ツー・ザ・ボトム・ビヘイビアを実装する方法は不明です。 –

答えて

1

私はXAMLだけではできないのではないかと恐れています。 イベントを監視し、コレクションが変更されたときにテキストボックスに行を追加するビヘイビアを作成できます。

汚れたとえば、あなたはMicrosoft.Xaml.Behaviors.Uwp.Managedパッケージを含める必要があります。

public class CommentsBehavior : Behavior 
{ 
    public ObservableCollection<string> Comments ... // you will need to make it a dependency property 

    protected virtual void OnAttached() 
    { 
     Comments.CollectionChanged += OnCollectionChanged; 
    } 

    protected virtual void OnDetaching() 
    { 
     Comments.CollectionChanged -= OnCollectionChanged; 
    } 

    private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
    { 
     if (e.NewItems != null) 
     { 
      foreach(string newItem in e.NewItems) 
      { 
       ((TextBox)AssociatedObject).Text = ((TextBox)AssociatedObject).Text + '\n' + newItem; 
      } 
     } 
    } 
} 

やスクロールのための

- UWP C# Scroll to the bottom of TextBox

しかし、なぜあなたは、このためにテキストボックスを使用したいのですか?リストを使用する方が理にかなっています。

+0

TextBoxにはTextChangedイベントがあるので、リンクに表示されているように、スクロール・ツー・ザ・ボトム効果を実装できます。 TextBoxがListView内に埋め込まれているこのシナリオでは、スクロール先の動作を実装する方法がわかりません。 –

+0

ObservableCollectionのイベントを使用できます。 –

+0

申し訳ありません、リンクを貼り付けずにコメントを投稿しました - http://stackoverflow.com/questions/2006729/how-can-i-have-a-listbox-auto-scroll-when-a-new-item-is-added –

関連する問題