INotifyPropertChanged Updating Bound Listview/gridview



好的,所以问题是我的 INotifyPropertyChanged 没有更新列表视图 n XAML

DiscoveredData.NetworkedComputersResults = NetworkedComputers;

ListView_LocalComputers.ItemsSource = DiscoveredData.NetworkedComputersResults; <使用它工作正常,我可以看到我的所有数据,但这显然不是这样做的方法。>

因为我知道我可以使用 ItemsSource 将数据加载到列表视图中,我认为问题出在 XAML 中。

如果有人能为我指出正确的方向,我会很棒。

另外,如果您发现我这样做不正确,请告知,我对这种编码语言相当陌生,并希望以正确的方式进行操作

提前谢谢你

<ListView Name="ListView_LocalComputers" ItemsSource="{Binding NetworkedComputerResults}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Status">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Border CornerRadius="2,2,2,2" Width="20" Height="20" Background="Transparent" BorderBrush="Transparent" Margin="3,3,3,3">
                            <Image HorizontalAlignment="Left" VerticalAlignment="Center" Width="12" Height="12" Source="{Binding Image}" Stretch="Fill" Margin="2,2,2,2"/>
                        </Border>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Local Computers" DisplayMemberBinding="{Binding ComputerName}">
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

//Constructor
public NetworkInformation()
{
    InitializeComponent();
    this.DataContext = DiscoveredData; //Defines the class to the view
    Discovery();
}
//Method
public void Discovery()
{
    GetIcon Icon = new GetIcon();
    BitmapImage IconOfComputer = null;
    List<DiscoveredComputer> NetworkedComputers = new List<DiscoveredComputer>();
    DirectoryEntry Discover = new DirectoryEntry("WinNT://Workgroup");
    BitmapImage On = Icon.LoadIcon(@"/Images/Icons/ComputerOn.ico");
    BitmapImage Off = Icon.LoadIcon(@"/Images/Icons/ComputerOff.ico");
    foreach (DirectoryEntry Node in Discover.Children)
    {
        try
        {
            if (Node.Properties.Count > 0)
            {
                IconOfComputer = On;
            }
        }
        catch
        {
            IconOfComputer = Off;
        }
        if (Node.Name != "Schema") { NetworkedComputers.Add(new DiscoveredComputer { Image = IconOfComputer, ComputerName = Node.Name, MyToolTip = "Node Type = " + Node.SchemaEntry.Name }); }
    }
    DiscoveredData.NetworkedComputersResults = NetworkedComputers;
    ListView_LocalComputers.ItemsSource = DiscoveredData.NetworkedComputersResults;
}
private class GetIcon
{
    public BitmapImage IconStorage { get; set; }
    public BitmapImage LoadIcon(String IconPath)
    {
        BitmapImage GeneratedIcon = new BitmapImage();
        GeneratedIcon.BeginInit();
        GeneratedIcon.UriSource = new Uri("pack://application:,,," + IconPath, UriKind.RelativeOrAbsolute);
        GeneratedIcon.EndInit();
        IconStorage = GeneratedIcon;
        return GeneratedIcon;
    }
}
public class NetworkData : INotifyPropertyChanged
{
    #region Property Notify Standard for all classes
    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
    #endregion
    #region Bound Data To View
    private List<DiscoveredComputer> _NetworkedComputersResults;
    public List<DiscoveredComputer> NetworkedComputersResults { 
        get { return _NetworkedComputersResults; }
        set
        {
            _NetworkedComputersResults = value;
            NotifyPropertyChanged("NetworkedComputersResults");
        }
    }
    #endregion
    public class DiscoveredComputer : NetworkData
    {
        public string ComputerName { get; set; }
        public BitmapImage Image { get; set; }
        public String MyToolTip { get; set; }
    }
}

你应该使用 ObservableCollection .它实现了INotifyCollectionChanged,用于通知集合何时更改,而不仅仅是单个项。

当然,项目本身应该实现INotifyPropertyChanged...

您正在将普通List<T>绑定到您的ListView。但是,这工作正常,它将无法满足添加/删除项目时动态更新列表视图的要求。

如果您需要在ListView中动态添加/删除元素,只需使用ObservableCollection<T>而不是List'。

private ObservableCollection<DiscoveredComputer> _NetworkedComputersResults;
public ObservableCollection<DiscoveredComputer> NetworkedComputersResults { 
    get { return _NetworkedComputersResults; }
    set
    {
        _NetworkedComputersResults = value;
        NotifyPropertyChanged("NetworkedComputersResults");
    }
}

如果你只需要动态添加/删除元素,那么可观察集合中的元素不需要实现INotifyPropertyChanged接口。

public class NetworkData 
{
    public NetworkData()
    {
        NetworkedComputersResults = new ObservableCollection<DiscoveredComputer>();
    }
    public ObservableCollection<DiscoveredComputer> NetworkedComputersResults{get;set;}
}

发现方法

public void Discovery()
{
    GetIcon Icon = new GetIcon();
    BitmapImage IconOfComputer = null;
    List<DiscoveredComputer> NetworkedComputers = new List<DiscoveredComputer>();
    DirectoryEntry Discover = new DirectoryEntry("WinNT://Workgroup");
    BitmapImage On = Icon.LoadIcon(@"/Images/Icons/ComputerOn.ico");
    BitmapImage Off = Icon.LoadIcon(@"/Images/Icons/ComputerOff.ico");
    foreach (DirectoryEntry Node in Discover.Children)
    {
        try
        {
            if (Node.Properties.Count > 0)
            {
                IconOfComputer = On;
            }
        }
        catch
        {
            IconOfComputer = Off;
        }
        if (Node.Name != "Schema") { NetworkedComputers.Add(new DiscoveredComputer { Image = IconOfComputer, ComputerName = Node.Name, MyToolTip = "Node Type = " + Node.SchemaEntry.Name }); }
    }
    //Use Clear and Add .Dont assign new instance DiscoveredData.NetworkedComputersResults=new ....
    DiscoveredData.NetworkedComputersResults.Clear();
    foreach (var item in NetworkedComputers)
    {
        DiscoveredData.NetworkedComputersResults.Add(item);
    }
}

我希望这会有所帮助。从我个人的角度来看,如果您在ViewModel中创建此发现方法并从ViewModel的构造函数调用它,那就太好了。似乎是双向通信,例如您从视图中设置 ViewModel 的属性,这是绑定而不是代码隐藏的工作

相关内容

  • 没有找到相关文章

最新更新