专注于绑定到集合的listview的最后一行 - wpf mvvm

时间:2017-05-25 06:46:08

标签: wpf mvvm data-binding wpf-controls

在一个wpf项目中,我将listview“绑定”到一个集合中 每次我将一个项目添加到集合中时我都希望将重点放在listview上的最后一行(添加到其中) 怎么做?

//XAML
<ListView
                x:Name="logListActions"
                Height="200"
                MinHeight="150"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch"
                Background="WhiteSmoke"
                BorderThickness="1"
                ItemsSource="{Binding LogMessages}">
                <ListView.View>
                    <GridView AllowsColumnReorder="True">
                        <GridViewColumn
                            Width="110"
                            DisplayMemberBinding="{Binding When}"
                            Header="Data" />
                        <GridViewColumn
                            Width="Auto"
                            DisplayMemberBinding="{Binding Message}"
                            Header="Messaggio" />
                    </GridView>
                </ListView.View>
            </ListView>



//ViewModel
public ObservableCollection<LogMessage> LogMessages
        {
            get { return _logMessageList; }
            set
            {
                _logMessageList = value;
                OnPropertyChanged("LogMessages");
            }
        }

2 个答案:

答案 0 :(得分:1)

将新项目添加到列表框后,从后面的代码中调用以下代码:

logListActions.ScrollIntoView(item);
logListActions.SelectedItem = item;

答案 1 :(得分:0)

Stream的{​​{1}}属性绑定到视图模型中的SelectedItem属性,并将后者设置为最后添加的ListView对象:

LogMessage
LogMessage

这将选择最后一行。然后,您可以处理视图中的<ListView ... ItemsSource="{Binding LogMessages}" SelectedItem="{Binding SelectedLogMessage}"> 事件以进行聚焦并突出显示它:

private LogMessage _selected;
public LogMessage SelectedLogMessage
{
    get { return _selected; }
    set { _selected = value; OnPropertyChanged("SelectedLogMessage"); }
}