簡體   English   中英

C#將ListView滾動到更新的項目

[英]C# Scroll ListView to Updated Item

我有一個ListView

    <ListView x:Name="BlockList" Grid.Row="1" Grid.ColumnSpan="4" ItemsSource="{Binding Blocks}">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="75" Header="Address" DisplayMemberBinding="{Binding Path=Address, Converter={StaticResource IntToHexConverter}}"/>
                <GridViewColumn Width="100" Header="Length" DisplayMemberBinding="{Binding Length}"/>
                <GridViewColumn Width="100" Header="Status" DisplayMemberBinding="{Binding Status}"/>
            </GridView>
        </ListView.View>
    </ListView>

顯示BlocksObservableCollection的地址,長度和狀態。

public class Block : ObservableObject
{
    public enum BlockStatus
    {
        Unwritten,
        Written,
        Verified,
        Bad,
    }

    private UInt16 address;
    public UInt16 Address
    {
        get { return address; }
        set { address = value; NotifyPropertyChanged(); }
    }

    private byte[] data;
    public byte[] Data
    {
        get { return data; }
        set { data = value; NotifyPropertyChanged(); }
    }

    private BlockStatus status;
    public BlockStatus Status
    {
        get { return status; }
        set { status = value; NotifyPropertyChanged(); }
    }

    public UInt16 Length
    {
        get { return (UInt16)Data.Length; }
    }

    public Block(UInt16 address, byte[] data) : this(address, data, BlockStatus.Unwritten) { }

    public Block(UInt16 address, byte[] data, BlockStatus status)
    {
        this.Address = address;
        this.Data = data;
        this.Status = status;
    }
}

這些Blocks最初將從BlockStatus.Unwritten狀態開始,最終被Written ,然后被Verified Status更改時,如何將ListView滾動到Block

示例窗口

有很多事情要做。

1)您的Block需要實現INotifyPropertyChanged

public class Block : ObservableObject, INotifyPropertyChanged
{
    // Your existing members and code


    private BlockStatus _status;
    public BlockStatus Status
    {
        get { return _status; }
        set { _status = value; NotifyPropertyChanged("Status"); }
    }

    // INotifyPropertyChanged implementation
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

2)您的ViewModel需要有一個StatusChanged事件

public delegate void StatusChangedHandler(object sender, StatusChangedHandlerArgs args);
public event StatusChangedHandler StatusChanged;

// Put this class where it suits your project
public class StatusChangedHandlerArgs
{
    public Block.BlockStatus NewStatus { get; private set; }

    public StatusChangedHandlerArgs(Block.BlockStatus newStatus)
    {
        NewStatus = newStatus;
    }
}

3) ViewModelBlocks屬性需要如下所示:

    private ObservableCollection<Block> _blocks;
    public ObservableCollection<Block> Blocks
    {
        get
        {
            return _blocks;
        }
        set
        {
            _blocks = value;
            if (_blocks != null)
            {
                foreach (var block in _blocks)
                {
                    block.PropertyChanged += block_PropertyChanged;
                }
            }
            NotifyPropertyChanged("Blocks");
        }
    }

   // The event handler for PropertyChanged 
    private void block_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName != "Status" || StatusChanged == null)
        {
            return;
        }

        var block = sender as Block;
        if (block != null)
        {
            StatusChanged.Invoke(block, new StatusChangedHandlerArgs(block.Status));
        }
    }

4)最后,您的View (具有ViewModel的實例)應在后面的代碼中訂閱StatusChanged事件。

   // Somewhere in the initializer or constructor for the page
   yourViewModel.StatusChanged += yourViewModel_StatusChanged;

   // The handler for StatusChanged
   private void yourViewModel_StatusChanged(object sender, StatusChangedHandlerArgs args)
    {
        var block = sender as Block;
        if (block != null)
        {
            BlockList.ScrollIntoView(block);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM