我正在构建一个WPF应用程序,该应用程序将填充来自各种新闻服务的过滤标题。每个标题触发一个事件,在控制台应用程序中我可以在控制台上显示该事件。我想在这里使用WPF,但是在这个尝试之前已经使用了bot。我的主窗口xaml如下所示。我最初的想法是有一个ObservableCollection在xaml的列表视图中填充列表项。如果这不是正确的方法,我会以更好的方式向专家征求意见,因为接收速度显示至关重要。如果我正在做的事情是正确的,那么我该如何将一个新的条目绑定到ObservableCollection来显示一个新的列表项?绑定事件到wpf列表实时更新
<StackPanel Orientation="Vertical" Margin="5,150 5 50" Name="HeadlinePanel">
<TextBlock Text="Filtered Headlines From Monitoring List"
HorizontalAlignment="Left" Margin="0,0 5 5" Name="ScrollingHeadlineLabel" FontWeight="Bold" FontSize="14" Background="LightSkyBlue" />
<ListBox>
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<Image Source="a property on the headline" />
<TextBlock><Run Text="headline is from a website"/></TextBlock>
</StackPanel>
</ListBoxItem>
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<Image Source="a property on the headline" />
<TextBlock><Run Text="headline is from TWTR"/></TextBlock>
</StackPanel>
</ListBoxItem>
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<Image Source="a property on the headline" />
<TextBlock><Run Text="headline from a different website"/></TextBlock>
</StackPanel>
</ListBoxItem>
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<Image Source="a property on the headline" />
<TextBlock><Run Text="text from a different tweet"/></TextBlock>
</StackPanel>
</ListBoxItem>
</ListBox>
</StackPanel>
在filteredStream.Start(控制台应用程序流传输开始时(如下所示代码)),但处理程序需要事先注册。在控制台应用程序中,我可以写入控制台(注释掉),但是在事件触发时,我将标题对象添加到集合中。我的问题是如何将它绑定到我的xaml列表项。我将从mainwindow方法启动流?或者我创建的一些方法在那里运行?
var config = new TwitterOAuthConfig()
{
ConsumerKey = customerKey,
ConsumerSecret = customerSecret,
AccessToken = accessToken,
AccessTokenSecret = accessTokenSecret,
GeoOnly = false,
KeywordsToMonitor = keywords,
UsersToFollow = followers
};
var filteredStream = new TwitterClient(config);
var headlineCollection = new ObservableCollection<Headline>();
// subscribe to the event handler
filteredStream.HeadlineReceivedEvent +=
(sender, arguments) => headlineCollection.Add(arguments.Headline);
//Console.WriteLine("ID: {0} said {1}", arguments.Headline.Username, arguments.Headline.HeadlineText);
filteredStream.ExceptionReceived += (sender, exception) => Console.WriteLine(exception.HeadlineException.ResponseMessage);
filteredStream.Start();
这是我原来的HeadlineViewModel
public class HeadlineViewModel : ObservableItem
{
private string _headlineText;
public string Source { get; set; }
public string Username { get; set; }
public string Text
{
get { return _headlineText; }
set
{
_headlineText = value;
RaisePropertyChangedEvent("HeadlineText");
}
}
public List<string> UrlsParsedFromText { get; set; }
public string TimeStamp { get; set; }
}
我也更新到以下几点:
public class HeadlineViewModel
{
public class HeadlineDisplayItems: ObservableItem
{
private string _headlineText;
public string HeadlineIconPath { get; set; }
public string TimeStamp { get; set; }
public string Username { get; set; }
public string Text
{
get { return _headlineText; }
set
{
_headlineText = value;
RaisePropertyChangedEvent("HeadlineText");
}
}
}
public List<string> UrlsParsedFromText { get; set; }
public ObservableCollection<HeadlineDisplayItems> HeadlineCollection { get; set; }
}
我有一个HeadlineViewModel,其中香港专业教育学院现在包括在后。所以viewmodel类需要标题的ObservableCollection?所以它可以绑定必要的属性? – dinotom
是的。而已。但是您需要注意的是,您必须为每个标题创建一个ViewModel,并为具有ObservableCollection的“Container”创建一个。然后通过属性ItemsSource将ListBox映射到“Container”集合。 Editted答案 –
我tihnk我拿到哪里youre去,虽然这是我的第一个WPF应用程序。我重做了Veiwmodel并将其张贴在原始帖子中。在PropertyChanged事件不知道应该只为标题,因为所有的displayitems每个标题 – dinotom