English 中文(简体)
C#/XAML/WPF绑定部分工作,仅显示列表中的第一项
原标题:C#/XAML/WPF binding working partially, only displays first item in List

我有一个非常简单的绑定,但我看到的问题是,我没有显示这三个公司(company_list是一个列表,其中company包含要绑定的company_id),而是看到弹出的窗口中只有company_list中的第一个company_id。我有其他绑定,它们似乎工作得很好,在其他一些情况下,我看到我使用了ItemSource而不是DataContext,但当我使用它时,我会得到“在使用ItemsSource之前,Items集合必须为空”。我在stackoverflow、msdn和其他地方广泛搜索了这个问题的简单答案,并看到了大多数我无法理解/应用的非常复杂的解决方案。

当我的窗口出现时,它具有:

公司A

它应该具有:

公司A
CompanyB
CompanyC

这是company_list的内容(是的,在调试器中验证)。建议不胜感激!代码和XAML紧随其后。

    ReadMasterCompanyList();  // populates a_state.company_list with 3 companies
    // display company list dialog
    CompanySelect cs_window = new CompanySelect();
    cs_window.CompanyListView.DataContext = a_state.company_list;
    // fails: cs_window.CompanyListView.ItemsSource = a_state.company_list;
    cs_window.Show();

以及来自CompanySelect的XAML:

<Grid>
    <ListView IsSynchronizedWithCurrentItem="True" 
      x:Name="CompanyListView"
       SelectionMode="Single" SelectionChanged="CompanyListView_SelectionChanged">
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="Height" Value="30"/>
            </Style>
        </ListView.ItemContainerStyle>
        <ListViewItem Content="{Binding Path=company_id}"></ListViewItem>
    </ListView>
</Grid>
最佳回答

首先,只在cs_window.Show()之后设置DataContext。

其次,您在ListView的XAML中作为子项的ListViewItem是您只看到一个的原因。

第三,如果您在XAML中定义ItemsSource,可能会更好地工作(并且更像MVVM),如下所示:

<ListView ItemsSource="{Binding Path=company_list}" ...>

这是在使a_state成为ListView的容器或某个其他祖先元素的DataContext之后。

问题回答

我会在codeehind中设置ListView的ItemsSource,而不是DataContext:

cs_window.CompanyListView.ItemsSource = a_state.company_list;

或具有约束力:

<ListView ItemsSource="{Binding company_list}">

然后改为设置ListView的ItemTemplate。

...
<ListView.ItemTemplate>
  <DataTemplate>
    <TextBlock Text="{Binding company_id}" />
  </DataTemplate>
</ListView.ItemTemplate>
...

我也会考虑使用MVVM设计模式用于可测试性和关注点分离,并考虑使用PascalCase作为属性名称。

此外,除非您特别想要ListView,否则我会使用ListBox。

The problem is, that you define one ListViewItem in your XAML code. You shouldn t do this.
Try something like this:

<Grid>
    <ListView IsSynchronizedWithCurrentItem="True" 
      x:Name="CompanyListView"
       SelectionMode="Single" SelectionChanged="CompanyListView_SelectionChanged">
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="Height" Value="30"/>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Content={Binding Path=company_id}/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签