【问题标题】:Binding ObservableCollection<Item> To TextBox (UWP)将 ObservableCollection<Item> 绑定到文本框 (UWP)
【发布时间】:2016-11-02 18:46:55
【问题描述】:

我想实现从“类结构”项目的 ObservableCollection 到具有 TextChanged 事件的 TextBox 的单向绑定。这个想法是,随着 Item 的 Comments 字段在 TextBox 中累积,TextBox 会自动向下滚动,以便始终显示最后一行。该集合绑定到 ListView,但我希望它以只读方式绑定到 TextBox。我不希望在 ResultViewModel 中添加其他方法,而是在 XAML 中添加。我该怎么做呢? TIA

// 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}"/>

【问题讨论】:

  • 准确理解你想要什么有点困难。您是否希望拥有一个用户无法修改的 TextBox,但 Comments 属性值会发生变化,并且您希望绑定的 TextBox 何时滚动到底部?
  • 没错,TextBox 不能被用户修改,但是随着 Comments 属性的积累,它超过了 TextBox,我希望 TextBox 滚动到底部,以便显示最后一行。这就是想要绑定到 TextBox 背后的动机。不确定如何在 TextBox 嵌入 ListView 的情况下实现滚动到底部的行为。

标签: c# xaml uwp


【解决方案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

但是你为什么要使用文本框呢?使用列表更有意义。

【讨论】:

  • 因为 TextBox 有 TextChanged 事件,我可以在其中实现滚动到底部的效果,如您的链接所示。我不确定如何在 TextBox 嵌入 ListView 的情况下实现滚动到底部的行为。
  • 您可以使用 ObservableCollection 中的事件。
  • 对不起,发表评论但没有粘贴链接 - stackoverflow.com/questions/2006729/…
猜你喜欢
  • 2011-09-15
  • 2017-05-31
  • 2010-11-06
  • 1970-01-01
  • 2016-04-20
  • 2018-08-13
  • 1970-01-01
  • 2019-09-23
  • 1970-01-01
相关资源
最近更新 更多