私の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"};
}
}
printline を実行すると、actionList が変更されますが、ListBox 項目が actionList に設定されません。UI が更新されない理由とその理由を知りたいですか? DataContext と ObservableCollections に変更する必要があることをどこかで読みましたが、それを試してみましたが、まだ更新されません。誰かが理由を知っていますか?