更新列表框项

本文关键字:列表 更新 | 更新日期: 2023-09-27 18:30:50

我有一个带有节点的树视图,所以当我单击其中一个节点时,我将使用绑定到可观察集合的项目更新列表框,这工作正常。接下来,当我单击树视图中的另一个节点时,我将不得不使用不同的数据(这也是一个 Obsv.Collection)更新列表框。关于如何进行此操作的任何想法?

<Resources>
    <DataTemplate x:Key="clipsource">
        <StackPanel>
            <Image Name="img" Width="125" Height="70" Margin="1,0" Source="{Binding Path=Path, Converter= {x:Static l:UriToThumbnailConverter.Instance}}"/>
            <TextBlock FlowDirection="LeftToRight" FontFamily="Arial" FontSize="12" Text="{Binding Path = DisplayClipName}"/>
        </StackPanel>
    </DataTemplate>
</Resources>
<Grid  Height="Auto" Grid.Column="2" VerticalAlignment="Stretch"  x:Name="RightPane_grid" Margin="2,0,0,0" KeyboardNavigation.DirectionalNavigation="None">
    <Grid.RowDefinitions>
        <RowDefinition Height="21.205"/>
        <RowDefinition  Height="*"/>
    </Grid.RowDefinitions>
    <Rectangle  Fill="#FF232323" Stroke="#FF000000" RadiusX="3" RadiusY="3" Margin="0,0,0,0" x:Name="Right_Pane_bkg" Grid.ColumnSpan="1" Grid.RowSpan="2" KeyboardNavigation.DirectionalNavigation="None"/>
    <ListBox Name="ListBox1" SelectionMode="Extended" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="0" Background="#FF3B3B3B"  Margin="2,25,2,2" ItemsSource="{Binding}" ItemTemplate="{StaticResource clipsource}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
</Grid>

代码隐藏:

private void Handle_Click1(object sender, MouseButtonEventArgs e) // tree view Item
{
    ListBox1.DataContext = Clips Items;
    // Clips Items is an Obs v. Collection 
}

我有另一个 Obs v 集合静止项目,我必须将其绑定到列表框以显示另一个视图

更新列表框项

您可以尝试以下操作:

    <TreeView Name="MyTreeView">
        <TreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="Header" Value="{Binding Path=Name}"/>
                <Setter Property="ItemsSource" Value="{Binding Path=SomeItems}"/>
            </Style>
        </TreeView.ItemContainerStyle>
    </TreeView>
    <ListBox ItemsSource="{Binding ElementName=MyTreeView, Path=SelectedItem.SomeItems}">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Content" Value="{Binding Path=Name}"/>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>

绑定将使用路径SomeItems从树视图中的选定项检索列表框中显示的项。这假设选定项如下所示:

public class MyTreeViewItem
{
    public string Name { get; private set; }
    public ObservableCollection<MyTreeViewItem> SomeItems { get; private set; }
    public MyTreeViewItem(string name)
    {
        if (name == null)
            throw new ArgumentNullException("name");
        this.SomeItems = new ObservableCollection<MyTreeViewItem>();
        this.Name = name;
    }
}

在树视图中选择项时,子项将显示在列表框中。

编辑:

若要填充树视图,请使用以下代码:

        MyTreeViewItem a = new MyTreeViewItem("A");
        a.SomeItems.Add(new MyTreeViewItem("A1"));
        a.SomeItems.Add(new MyTreeViewItem("A2"));
        a.SomeItems.Add(new MyTreeViewItem("A3"));
        MyTreeViewItem b = new MyTreeViewItem("B");
        b.SomeItems.Add(new MyTreeViewItem("B1"));
        b.SomeItems.Add(new MyTreeViewItem("B2"));
        b.SomeItems.Add(new MyTreeViewItem("B3"));
        this.MyTreeView.ItemsSource = new MyTreeViewItem[] { a, b };