【问题标题】:How to update ListBoxItem property如何更新 ListBoxItem 属性
【发布时间】:2014-01-22 20:39:08
【问题描述】:

我想更新绑定到 ListBoxItem 中的 TextBox 的 DownloadingSpeed 属性。我在 c# 中生成 ListBoxItems。请指导我如何在 C# 中更新 ListItemBox 中 TextBoxes 的绑定内容。

WPF LisBox 代码

 <ListBox Width="Auto" 
                 Name="WebsiteList"
                 MouseUp="SelectedListItem"
                 SelectedIndex="0"
                 Grid.Column="1" 
                 Grid.Row="2" 
                 Grid.RowSpan="2"
                 Margin="0,0,0,0">
            <ListBox.ItemTemplate>
            <DataTemplate>
                    <Grid Width="920">
                    <StackPanel Orientation="Vertical">
                            <StackPanel Orientation="Horizontal">
                                <Grid Width="920">
                                <TextBlock FontWeight="Bold" FontSize="18" Width="Auto">
                                <Hyperlink NavigateUri="http://google.com" FontStyle="Italic">
                                    <Label Content="{Binding WebsiteTitle}" /><Label FontSize="10" Margin="0,0,0,3" Content="{Binding DirectorySize}" />
                                </Hyperlink>
                                </TextBlock>

                                <TextBlock Width="0" TextAlignment="right">
                                    <TextBlock Visibility="Hidden" Text="{Binding DownloadID}"/>
                                    <TextBlock Visibility="Hidden" Text="{Binding DirectoryPath}"/>
                                    <TextBlock Visibility="Hidden" Text="{Binding CurrentTime}"/>
                                    <TextBlock Visibility="Hidden" Text="{Binding CurrentDate}"/>
                                    <TextBlock Visibility="Hidden" Text="{Binding GivenUrl}"/>
                                </TextBlock>
                                </Grid>

                            </StackPanel>

                        <StackPanel Orientation="Horizontal">
                                <Grid Width="920">
                                    <ProgressBar Name="progress1" Maximum="100" Minimum="0" Value="30" Background="#FFF" Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=Width}" Height="10" />
                                </Grid>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                                <Grid Width="920">
                            <TextBlock HorizontalAlignment="Left" Width="Auto">Status: <TextBlock Text="{Binding Status}"/></TextBlock>
                                    <TextBlock Width="Auto" TextAlignment="right">
                                    <TextBlock Text="Downloading Speed: "/> 
                                    <TextBlock Name="DownloadingSpeed" Text="{Binding DownloadingSpeed}"/>
                            </TextBlock>
                                </Grid>
                            </StackPanel>
                    </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
         </ListBox> 

在 MainWindow.xaml.cs 中

 private void GenerateList()
{
 List<Download> WebsitesList = new List<Download>();

 WebsitesList.Add(new Download() {DownloadID = ID, WebsiteTitle = WebTitle, Status = WebStatus, CurrentTime = CurrentTime,CurrentDate = CurrentDate, DownloadingSpeed = DownloadSpeed, DirectoryPath = path, DirectorySize = helper.GetDirectorySize(path),GivenUrl = url });

 WebsiteList.ItemsSource = WebsitesList;       
}
//get download speed and update DownloadingSpeed    
 private void updateDownloadingSpeed(object sender, EventArgs e)
    {

        interfaceStats = NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics();

        downloadspeed = (interfaceStats.BytesReceived - previousbytesreceived) / 1024;

        previousbytesreceived = NetworkInterface.GetAllNetworkInterfaces()[0].GetIPv4Statistics().BytesReceived;

        Download.DownloadingSpeed = Math.Round(downloadspeed, 2) + " KB/s"; //Rounding to 2 decimal places and save in DownloadSpeed Property
}

在下载.cs中

 public class Download : INotifyPropertyChanged
    {
          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));
        }

}

【问题讨论】:

  • 什么触发updateDownloadingSpeed事件?
  • updateDownloadingSpeed() 方法定期调用以获取下载速度。我正在使用 System.Net.NetworkInformation 来获取下载速度。
  • 您的代码似乎都可以。您需要调试您的代码...在OnPropertyChanged 处理程序、DownloadingSpeed 设置器和updateDownloadingSpeed 方法中放置断点。他们都被击中了吗?在 Visual Studio 的输出窗口中你有什么错误。
  • 我没有任何错误。我在 c# 中获得了下载速度,但它没有更新 TextBox 中的 wpf 绑定属性 DownloadingSpeed。我必须在 wpf listboxitem 中显示下载速度。

标签: c# wpf xaml


【解决方案1】:

为 TextBlock 添加 UpdateSourceTrigger。

<TextBlock Name="DownloadingSpeed" Text="{Binding DownloadingSpeed, UpdateSourceTrigger=PropertyChanged}"/>

【讨论】:

  • -1 你明明不懂UpdateSourceTrigger
  • @HighCore,不像你说-1,而是不是投反对票。
  • @Sheridan,你错了。反对票已经过了,但消失了。
  • @ValeraScherbakov,我没有错了。您的回答(奇怪地)有 no 反对票。也许 HighCore 投了反对票,然后将其删除,但截至目前,您的回答没有反对票。
  • @Sheridan,我知道我的回答现在还没有被否决。但它已经过去了。当我写这篇评论时,投反对票是……
【解决方案2】:

可能会发现,如果您通过定义属性和正确声明 XAML 来正确使用 WPF,您的代码就可以工作。通常有一个 ObservableCollection&lt;T&gt; 属性,您可以将数据绑定到 UI 集合控件。然后,您将数据绑定到控件的ItemsSource 属性:

<ListBox ItemsSource="{Binding Items}" ... />

然后,当您想要更新项目的任何属性时,您需要设置项目的属性。这个:

Download.DownloadingSpeed = Math.Round(downloadspeed, 2) + " KB/s";

...不是设置集合中项目的属性。为此,您需要执行以下操作:

// Use whatever filter you want to find the correct item from the collection
Download downloadInstance = Items.Where(d => d.Name == "SomeName").First();
downloadInstance.DownloadingSpeed = Math.Round(downloadspeed, 2) + " KB/s";

【讨论】:

  • 不起作用:(当前上下文中不存在名称“项目”我也尝试过下载downloadInstance = WebsiteList.Where(d => d.DownloadID == ID).First() ;
  • 通常有一个 ObservableCollection 属性,您可以将数据绑定到 UI 集合控件。 这意味着 需要创建像这样的财产。我是在告诉你怎么做,而不是为你做。
猜你喜欢
  • 1970-01-01
  • 2012-02-14
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
  • 2011-02-14
  • 2017-01-20
  • 1970-01-01
  • 2021-09-25
相关资源
最近更新 更多