C# DataTemplate未生成ListItemBox

C# DataTemplate未生成ListItemBox,c#,wpf,xaml,datatemplate,C#,Wpf,Xaml,Datatemplate,我想使用DataTemplate生成ListItemBox,但未生成项。请告诉我哪里错了。我在MainWindow.xaml中有以下代码 <Window x:Class="Offline_Website_Downloader.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006

我想使用DataTemplate生成ListItemBox,但未生成项。请告诉我哪里错了。我在MainWindow.xaml中有以下代码

<Window x:Class="Offline_Website_Downloader.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:bd="clr-namespace:Offline_Website_Downloader"
    Title="Offline Website Downloader" Background="#f5f6f7" Height="500" Width="800"
    WindowStartupLocation="CenterScreen">

<Window.Resources>

    <bd:BindingController x:Key="BindingControllerKey"  />

    <DataTemplate x:Key="DownloadedWebsitesListBox">

            <StackPanel Orientation="Vertical">
                <StackPanel Orientation="Horizontal" Width="Auto">

                    <TextBlock FontWeight="Bold" FontSize="18" Width="480">
                            <Hyperlink NavigateUri="http://google.com">
                                <Label Content="{Binding Path=WebsiteTitle}" />
                            </Hyperlink>
                    </TextBlock>

                    <TextBlock Width="132" TextAlignment="right">
                            <TextBlock Text="Remaining Time: "/>
                            <TextBlock Name="TimeRemaining" Text="js"/>
                </TextBlock>

                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <ProgressBar Name="progress1" Maximum="100" Minimum="0" Value="30" Background="#FFF" Width="612" Height="10" />
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                <TextBlock HorizontalAlignment="Left" Width="450">Status: <TextBlock Text="{Binding Path=Status}"/></TextBlock>
                    <TextBlock Width="162" TextAlignment="right">
                                    <TextBlock Text="Downloading Speed: "/>
                                    <TextBlock Name="DownloadingSpeed" Text="{Binding Path=DownloadingSpeed}"/>
                            </TextBlock>
                </StackPanel>
            </StackPanel>

    </DataTemplate>

</Window.Resources>

<Grid>
<ListBox Width="Auto" 
                 Name="WebsiteList"
                 Grid.Column="1" 
                 Grid.Row="2" 
                 Grid.RowSpan="2"
                 ItemsSource="{Binding}"
                 ItemTemplate="{StaticResource DownloadedWebsitesListBox}"
                 Margin="0,0,0,0">
</ListBox>
</Grid>
</window>
和BindingController.cs

   public class BindingController
   {
    public BindingController()
    {

    }

    private string _WebsiteTitle;
    public string WebsiteTitle
    {
        set { _WebsiteTitle = value; }
        get { return _WebsiteTitle; }
    }

    private string _Status;
    public string Status
    {
        set { _Status = value ; }
        get { return _Status ; }
    }

    private string _DownloadStartDate;
    public string DownloadStartDate
    {
        set { _DownloadStartDate = value; }
        get { return _DownloadStartDate ; }
    }

    private string _DownloadingSpeed = "0 kb/s";
    public string DownloadingSpeed
    {
        set { _DownloadingSpeed = value; }
        get { return _DownloadingSpeed; }
    }

}

您的问题是将
列表框
绑定到一个包含多个属性但实际上只表示单个对象/状态的对象。
列表框
希望显示项目列表(即
IList
IBindingList
IEnumerable
ObservableCollection

假设您希望一次显示多个下载,我假设您使用的是
列表框
,我会将下载属性重构为一个单独的类。您还需要在属性上实现
INotifyPropertyChanged
,以便在更改值时,它们将显示在UI中

public class Download : INotifyPropertyChanged
{
    private string _WebsiteTitle;
    public string WebsiteTitle
    {            
        get { return _WebsiteTitle; }
        set
        {
            if (_WebsiteTitle == value)
                return;

            _WebsiteTitle = value;
            this.OnPropertyChanged("WebsiteTitle");
        }
    }

    private string _Status;
    public string Status
    {
        get { return _Status; }
        set
        {
            if (_Status == value)
                return;

            _Status = value;
            this.OnPropertyChanged("Status");
        }
    }

    private string _DownloadStartDate;
    public string DownloadStartDate
    {
        get { return _DownloadStartDate; }
        set
        {
            if (_DownloadStartDate == value)
                return;

            _DownloadStartDate = value;
            this.OnPropertyChanged("DownloadStartDate");
        }
    }

    private string _DownloadingSpeed = "0 kb/s";
    public string DownloadingSpeed
    {
        get { return _DownloadingSpeed; }
        set
        {
            if (_DownloadingSpeed == value)
                return;

            _DownloadingSpeed = value;
            this.OnPropertyChanged("DownloadingSpeed");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if(this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
新的
BindingController

public class BindingController
{
    public BindingController()
    {
        this.Downloads = new ObservableCollection<Download>();
    }

    public ObservableCollection<Download> Downloads { get; private set; }
}

您的问题是将
列表框
绑定到一个包含多个属性但实际上只表示单个对象/状态的对象。
列表框
希望显示项目列表(即
IList
IBindingList
IEnumerable
ObservableCollection

假设您希望一次显示多个下载,我假设您使用的是
列表框
,我会将下载属性重构为一个单独的类。您还需要在属性上实现
INotifyPropertyChanged
,以便在更改值时,它们将显示在UI中

public class Download : INotifyPropertyChanged
{
    private string _WebsiteTitle;
    public string WebsiteTitle
    {            
        get { return _WebsiteTitle; }
        set
        {
            if (_WebsiteTitle == value)
                return;

            _WebsiteTitle = value;
            this.OnPropertyChanged("WebsiteTitle");
        }
    }

    private string _Status;
    public string Status
    {
        get { return _Status; }
        set
        {
            if (_Status == value)
                return;

            _Status = value;
            this.OnPropertyChanged("Status");
        }
    }

    private string _DownloadStartDate;
    public string DownloadStartDate
    {
        get { return _DownloadStartDate; }
        set
        {
            if (_DownloadStartDate == value)
                return;

            _DownloadStartDate = value;
            this.OnPropertyChanged("DownloadStartDate");
        }
    }

    private string _DownloadingSpeed = "0 kb/s";
    public string DownloadingSpeed
    {
        get { return _DownloadingSpeed; }
        set
        {
            if (_DownloadingSpeed == value)
                return;

            _DownloadingSpeed = value;
            this.OnPropertyChanged("DownloadingSpeed");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if(this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
新的
BindingController

public class BindingController
{
    public BindingController()
    {
        this.Downloads = new ObservableCollection<Download>();
    }

    public ObservableCollection<Download> Downloads { get; private set; }
}

您的问题是将
列表框
绑定到一个包含多个属性但实际上只表示单个对象/状态的对象。
列表框
希望显示项目列表(即
IList
IBindingList
IEnumerable
ObservableCollection

假设您希望一次显示多个下载,我假设您使用的是
列表框
,我会将下载属性重构为一个单独的类。您还需要在属性上实现
INotifyPropertyChanged
,以便在更改值时,它们将显示在UI中

public class Download : INotifyPropertyChanged
{
    private string _WebsiteTitle;
    public string WebsiteTitle
    {            
        get { return _WebsiteTitle; }
        set
        {
            if (_WebsiteTitle == value)
                return;

            _WebsiteTitle = value;
            this.OnPropertyChanged("WebsiteTitle");
        }
    }

    private string _Status;
    public string Status
    {
        get { return _Status; }
        set
        {
            if (_Status == value)
                return;

            _Status = value;
            this.OnPropertyChanged("Status");
        }
    }

    private string _DownloadStartDate;
    public string DownloadStartDate
    {
        get { return _DownloadStartDate; }
        set
        {
            if (_DownloadStartDate == value)
                return;

            _DownloadStartDate = value;
            this.OnPropertyChanged("DownloadStartDate");
        }
    }

    private string _DownloadingSpeed = "0 kb/s";
    public string DownloadingSpeed
    {
        get { return _DownloadingSpeed; }
        set
        {
            if (_DownloadingSpeed == value)
                return;

            _DownloadingSpeed = value;
            this.OnPropertyChanged("DownloadingSpeed");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if(this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
新的
BindingController

public class BindingController
{
    public BindingController()
    {
        this.Downloads = new ObservableCollection<Download>();
    }

    public ObservableCollection<Download> Downloads { get; private set; }
}

您的问题是将
列表框
绑定到一个包含多个属性但实际上只表示单个对象/状态的对象。
列表框
希望显示项目列表(即
IList
IBindingList
IEnumerable
ObservableCollection

假设您希望一次显示多个下载,我假设您使用的是
列表框
,我会将下载属性重构为一个单独的类。您还需要在属性上实现
INotifyPropertyChanged
,以便在更改值时,它们将显示在UI中

public class Download : INotifyPropertyChanged
{
    private string _WebsiteTitle;
    public string WebsiteTitle
    {            
        get { return _WebsiteTitle; }
        set
        {
            if (_WebsiteTitle == value)
                return;

            _WebsiteTitle = value;
            this.OnPropertyChanged("WebsiteTitle");
        }
    }

    private string _Status;
    public string Status
    {
        get { return _Status; }
        set
        {
            if (_Status == value)
                return;

            _Status = value;
            this.OnPropertyChanged("Status");
        }
    }

    private string _DownloadStartDate;
    public string DownloadStartDate
    {
        get { return _DownloadStartDate; }
        set
        {
            if (_DownloadStartDate == value)
                return;

            _DownloadStartDate = value;
            this.OnPropertyChanged("DownloadStartDate");
        }
    }

    private string _DownloadingSpeed = "0 kb/s";
    public string DownloadingSpeed
    {
        get { return _DownloadingSpeed; }
        set
        {
            if (_DownloadingSpeed == value)
                return;

            _DownloadingSpeed = value;
            this.OnPropertyChanged("DownloadingSpeed");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if(this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
新的
BindingController

public class BindingController
{
    public BindingController()
    {
        this.Downloads = new ObservableCollection<Download>();
    }

    public ObservableCollection<Download> Downloads { get; private set; }
}

非常感谢你。最佳答案。我学到了很多。再次感谢。:)我用过你的密码。这将用新内容替换以前创建的ListItemBox的内容。请告诉我如何解决这个问题?我不知道你的意思,你的意思是它改变了你应用程序中的所有列表框,而不仅仅是你试图应用它的列表框?如果是这样的话,您是否在某个地方使用了一种无眼的
样式
,可以将其自身应用于所有ListBoxItems@Zohaibt非常感谢你。最佳答案。我学到了很多。再次感谢。:)我用过你的密码。这将用新内容替换以前创建的ListItemBox的内容。请告诉我如何解决这个问题?我不知道你的意思,你的意思是它改变了你应用程序中的所有列表框,而不仅仅是你试图应用它的列表框?如果是这样的话,您是否在某个地方使用了一种无眼的
样式
,可以将其自身应用于所有ListBoxItems@Zohaibt非常感谢你。最佳答案。我学到了很多。再次感谢。:)我用过你的密码。这将用新内容替换以前创建的ListItemBox的内容。请告诉我如何解决这个问题?我不知道你的意思,你的意思是它改变了你应用程序中的所有列表框,而不仅仅是你试图应用它的列表框?如果是这样的话,您是否在某个地方使用了一种无眼的
样式
,可以将其自身应用于所有ListBoxItems@Zohaibt非常感谢你。最佳答案。我学到了很多。再次感谢。:)我用过你的密码。这将用新内容替换以前创建的ListItemBox的内容。请告诉我如何解决这个问题?我不知道你的意思,你的意思是它改变了你应用程序中的所有列表框,而不仅仅是你试图应用它的列表框?如果是这样的话,您是否在某个地方使用了一种无眼的
样式
,可以将其自身应用于所有ListBoxItems@佐哈伊布