在我的 Xaml 我有这个,
<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>
在我的数据类中,我有这个,
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 但我尝试了那个,但它仍然没有更新。有谁知道为什么?