簡體   English   中英

ListView在運行時不會更新

[英]ListView does not update at run-time

我整天都在嘗試,但是沒有運氣。 我能夠顯示UserControl,但是即使我在XAML中進行了設置,我也無法弄清為什么不顯示列。 另外,我不確定數據是否顯示,因為表格打開空白。

這是我從另一個程序調用的類庫的一部分。 但是,它始終顯示空白表格,並且沒有任何更新。

以下是我的UserControl代碼。

public partial class DisplayData : UserControl
{
    ObservableCollection<Data> _DataCollection = new ObservableCollection<Data>();

    public DisplayData()
    {
        InitializeComponent();

        Window window = new Window();
        window.Content = new UserControl();
        window.Show();
    }

    public void AddDataToList(int ID, string Name)
    {
        _DataCollection.Add(new Data
        {
            StudentID = ID,
            StudentName = Name
        });
    }

    public ObservableCollection<Data> DataCollection { get { return _DataCollection; } }
}

public struct Data
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
}

以下是我的稱呼方式:

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        DisplayData displayData = new DisplayData();
        displayData.AddDataToList(2, "John");
        System.Threading.Thread.Sleep(5000);
    }
}

和xaml:

<UserControl x:Class="ConsoleApplication1.DisplayData"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="100" d:DesignWidth="210">
    <StackPanel>
        <ListView ItemsSource="{Binding DataCollection}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="100" Header="ID" DisplayMemberBinding="{Binding StudentID}"  />
                    <GridViewColumn Width="100" Header="Name" DisplayMemberBinding="{Binding StudentName}" />
                </GridView>
            </ListView.View>
        </ListView>
    </StackPanel>
</UserControl>

編輯我還想補充一點:

  1. 它不會顯示正確的尺寸
  2. 我什至看不到表格上的列標題
  3. 也沒有數據更新。

另外,不確定這是否有幫助,但是我已將其作為WPF UserControl添加到我的現有項目中。

如果不查看所有代碼,就無法確定這是問題(還是唯一問題),但是您需要綁定DataContext屬性。 您的listview知道這些列已綁定到某些屬性,但是他們不知道這些屬性從何而來(該類)。

您還將同樣需要研究INotifyPropertyChanged(也許不是針對該項目,而是一般而言)。 有點解釋,所以您可以查看它的功能,但這是WPF綁定的很大一部分。

http://msdn.microsoft.com/zh-CN/library/system.windows.frameworkelement.datacontext.aspx

最好的方法是找到有關綁定以及如何設置列表視圖的良好教程。 如果沒有一些背景知識,很難真正弄清楚。 無論如何是我的。 希望這會有所幫助。

我不是XAML專業人士,但在文檔中,他們有不同的綁定文本: http : //msdn.microsoft.com/zh-cn/library/ms750972.aspx

嘗試

DisplayMemberBinding="{Binding Path=StudentID}"

您是否嘗試過改變

public struct Data 

進入

public class Data

我今天也整天都在研究ListView,如果有什么我能說的是它很挑剔。 它可能只是不想接受一個結構。

編輯:這是我的工作ListView

<ListView Grid.Column="2" HorizontalAlignment="Stretch" Margin="0" Name="listTableItem" VerticalAlignment="Stretch">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Name"
                DisplayMemberBinding="{Binding Name}"
                Width="75"
            />
            <GridViewColumn Header="Value"
                DisplayMemberBinding="{Binding Value}"
                Width="500"
            />
        </GridView>
    </ListView.View>
</ListView>

我認為您需要將DataContext綁定到背后的代碼。 見下文。

<UserControl x:Class="ConsoleApplication1.DisplayData"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         DataContext="{Binding RelativeSource={RelativeSource Self}}" 
         mc:Ignorable="d" 
         d:DesignHeight="100" d:DesignWidth="210">

就像其他海報所說的那樣,您需要設置DataContext(如Figabytes答案):

<UserControl x:Class="ConsoleApplication1.DisplayData"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         DataContext="{Binding RelativeSource={RelativeSource Self}}" 
         mc:Ignorable="d" 
         d:DesignHeight="100" d:DesignWidth="210">

另一個問題是您沒有嘗試顯示控件。 下面的代碼將創建一個窗口對象,並將您創建的控件作為內容放置。 之后,您只需調用Show()或ShowDialog(),您就應該得到工作的結果。

[STAThread]
static void Main(string[] args)
{
        DisplayData displayData = new DisplayData();
        displayData.AddDataToList(2, "John");

        Window window = new Window();
        window.Content = displayData;

        window.ShowDialog();
}

暫無
暫無

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

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