Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何显示ObservableCollection<;T>;在列表框中_Wpf_Mvvm_Binding - Fatal编程技术网

Wpf 如何显示ObservableCollection<;T>;在列表框中

Wpf 如何显示ObservableCollection<;T>;在列表框中,wpf,mvvm,binding,Wpf,Mvvm,Binding,我无法绑定到我的列表框中的ObservableCollection 我使用的是MVVM,WPF 页面的绑定工作正常。我的理解是,网格(未在代码中显示)绑定到我的DataContext,这是我的ViewModel。因此,我的ListBox可以通过Itemssource绑定到名为Folders的对象。我的文件夹对象非常简单,它是字面上的 private ObservableCollection<Folders> _folders; public ObservableCollection&

我无法绑定到我的列表框中的
ObservableCollection

我使用的是MVVM,WPF

页面的绑定工作正常。我的理解是,网格(未在代码中显示)绑定到我的
DataContext
,这是我的ViewModel。因此,我的
ListBox
可以通过
Itemssource
绑定到名为Folders的对象。我的文件夹对象非常简单,它是字面上的

private ObservableCollection<Folders> _folders;
public ObservableCollection<Folders> Folders 
{
get { return _folders; }
set
  {
      if (value == _folders)
      return;

      _folders = value;
      OnPropertyChanged("Folders");
   }
}
最后,我的XAML

    <ListBox Grid.RowSpan="2" ItemsSource="{Binding Folders, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" SelectedItem="{Binding SelectedFolderItem}" IsSynchronizedWithCurrentItem="True">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}, AncestorLevel=1}, Path=DataContext}">
                    <StackPanel>
                        <TextBlock Text="{Binding SourceFolder}" />
                        <TextBlock Text="{Binding DestinationFolder}" />
                        <Button Content="Edit" Command="{Binding EditCommand}" CommandParameter="{Binding SelectedListItem}"/>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

按钮绑定/执行,但2个文本块不绑定/执行。我也试过了

 <TextBlock Text="{Binding Folders.SourceFolder}" />
 <TextBlock Text="{Binding Folders.DestinationFolder}" />

但这是同一个问题。内容不显示(不绑定),因为如果我在ViewModel上添加手表,我可以看到值与它们应该的一样

如果有帮助,如果我将代码更新为

 <TextBlock Text="{Binding SelectedFolderItem.SourceFolder}" />
 <TextBlock Text="{Binding SelectedFolderItem.DestinationFolder}" />

然后它就可以工作了,尽管这不是我们想要的(它只是循环了正确的次数,但只针对1项!)


有人能给我指出正确的方向吗?

您正在设置不同的DataContext。您不需要这样做,否则会破坏项目模板的用途

<StackPanel Orientation="Horizontal">
    <StackPanel>
        <TextBlock Text="{Binding SourceFolder}" />
        <TextBlock Text="{Binding DestinationFolder}" />
        <Button Content="Edit"
                Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}, Path=DataContext.EditCommand}"
                CommandParameter="{Binding}"/>
    </StackPanel>
</StackPanel>


当然,如果您希望按钮也能正常工作,只需将当前拥有的相对源移动到按钮的绑定。

为什么更改了它们的数据上下文?如果没有不同的数据上下文,按钮将不会执行。您只需更改@dowhilefor指出的需要它的绑定的相对源。根据经验,如果你玩
DataContext
,你可能做错了。@user7116,谢谢,我刚刚意识到了这一点。谢谢,但是,如果我删除了“第二个”DataContext,那么我的按钮就不会绑定/执行。这确实解决了我的问题,但创建了这个新问题!我更新了我的答案。请记住,只要不覆盖DataContext,它就会被继承,正如您在最初的文章中所做的那样。但为了让你的命令发挥作用,你需要它。因此,您只需为绑定传递一个不同的源,它就会忽略当前的DataContext。。。我真的不明白绑定在做什么。。。这就像一下子掉了一千个便士。关于这一点,谢谢你检查我的代码:)通常通过使用当前的DataContext绑定工作,但是你可以用ElementName、Source或RelativeSource轻松地更改它。在本例中,我们仅使用RelativeSource查找列表框,获取DataContext并绑定到其EditCommand属性。为什么要绑定到编辑按钮的选定项?如果您想让它编辑当前项,只需使用
CommandParameter=“{Binding}”
,这是正在模板化的当前项的快捷方式(-ish)。
<StackPanel Orientation="Horizontal">
    <StackPanel>
        <TextBlock Text="{Binding SourceFolder}" />
        <TextBlock Text="{Binding DestinationFolder}" />
        <Button Content="Edit"
                Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}, Path=DataContext.EditCommand}"
                CommandParameter="{Binding}"/>
    </StackPanel>
</StackPanel>