ListBox 不显示绑定数据 [英] ListBox does not display the binding data
本文介绍了ListBox 不显示绑定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的 Xaml 中,我有这个,
In my Xaml i have this,
<ComboBox x:Name="masterCB" HorizontalAlignment="Left" Margin="5,127,0,0" VerticalAlignment="Top" Width="106" Height="22" Background="White" ItemsSource="{Binding Masters}" FontSize="11" SelectedIndex="{Binding Action}"/>
<Label Content="Select Action:" HorizontalAlignment="Left" VerticalAlignment="Top" Height="26" Width="116" Margin="0,151,0,0" Background="{x:Null}" FontWeight="Bold"/>
<ListBox x:Name="actionBox" HorizontalAlignment="Left" Height="250" VerticalAlignment="Top" Width="116" Margin="5,177,0,0" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding ActionList}" AlternationCount="2" MouseDoubleClick="actionBox_DoubleClick">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="{x:Null}">
<TextBlock Text="{Binding}" TextTrimming="WordEllipsis" TextWrapping="Wrap" Height="40" FontSize="11" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="Silver"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
在我的数据类中,我有这个,
In my data class i have this,
private List<String> actionList;
public int Action
{
get{return this.action;}
set
{
action = value;
populateActionList();
}
}
public List<String> ActionList
{
get { return actionList; }
set { this.actionList = value; }
private void populateActionList()
{
if (this.action == 0)
{
actionList = new List<String> {
"Chinese",
"Indian",
"Malay",
"Indian",
};
if (this.action == 1)
{
actionList = new List<String> {
"Dog",
"Cats",
"Pigs",
"Horses",
"Fish",
"Lion"};
}
}
当我执行打印行时,我的 actionList 已更改,但 ListBox 项未设置为我的 actionList.只想知道 UI 不更新的原因和原因?我在某处读到您必须更改为 DataContext 和 ObservableCollections,但我尝试了那个,但它仍然没有更新.有谁知道为什么?
When i do a printline, my actionList is changed, but the ListBox item is not set to my actionList. Would just like to know why and how come the UI does not update? I read somewhere that you would have to change to DataContext and ObservableCollections but i tried that one out and it still does not update. Does anyone know why?
推荐答案
将您的代码更改为以下内容,将达到您的预期
Changing your code to the following, will do what you expect
private ObservableCollection<String> _actionList;
public ObservableCollection<String> ActionList {
get {
if (_actionList == null) {
_actionList = new ObservableCollection<String>();
}
return actionList;
}
}
private void populateActionList(){
if (this.action == 0) {
ActionList.Clear();
ActionList.Add("Chinese");
ActionList.Add("Indian");
ActionList.Add("Malay");
ActionList.Add("Indian");
}
if (this.action == 1){
ActionList.Clear();
ActionList.Add("Dog");
ActionList.Add("Cats");
ActionList.Add("Pigs");
ActionList.Add("Horses");
ActionList.Add("Fish");
ActionList.Add("Lion");
}
}
这篇关于ListBox 不显示绑定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文