0

私は自分のクラスによって動的に入力される ListBox を持っています。これは私のリストボックスの例です:

<ListBox x:Name="mylistbox" SelectionChanged="timelinelistbox_SelectionChanged_1">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <TextBlock Text="{Binding userid}" Visibility="Collapsed" />
                <TextBlock Text="{Binding postid}" Visibility="Collapsed" />
                <Image Source="{Binding thumbnailurl}" />
                <TextBlock Text="{Binding username}" />
                <TextBlock Text="{Binding description}" />
                <Image Source="{Binding avatar}" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

ListBox の SelectedItemChanged イベントがトリガーされると、ListBoxItem を取得します。しかし今、その ListBoxItem の子を変更したいのですが、ListBoxItem の子にアクセスできないようです。

私は試した:

private void timelinelistbox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
    //Get the data object that represents the current selected item
    MyOwnClass data = (sender as ListBox).SelectedItem as MyOwnClass;

    //Get the selected ListBoxItem container instance    
    ListBoxItem selectedItem = this.timelinelistbox.ItemContainerGenerator.ContainerFromItem(data) as ListBoxItem;

    // change username and display
    data.username = "ChangedUsername";
    selectedItem.Content = data;
}

しかし、ユーザー名は変更されません...

4

1 に答える 1