在 WPF 中的 ListView 中填充 ComboBox

发布于 2024-10-18 23:14:56 字数 1319 浏览 0 评论 0原文

我已在 ListView 内填充了 ComboBox。屏幕截图如下 在此处输入图像描述

如上所示,它显示“M”、“a”、“c”而不是“Mac”。为什么要将单词分成字符?

在我编写的代码隐藏文件中

ItemCategoryDAL itemCategoryDalObj = new ItemCategoryDAL();
            DataTable dataTable = itemCategoryDalObj.GetAllItemCategory();

            listView1.ItemsSource = dataTable.DefaultView;

在我编写的 .xaml 文件中:

<ListView  Height="148" HorizontalAlignment="Left" Margin="23,12,0,0" Name="listView1" VerticalAlignment="Top" Width="447" >
  <ListView.View>
     <GridView>
        - - - - - - - - 
        - - - - - - - -
        <GridViewColumn Header="Category Name" Width="150">
           <GridViewColumn.CellTemplate>
               <DataTemplate>
                  <ComboBox ItemsSource="{Binding Path=IC_NAME }" Width="120" />
               </DataTemplate>
           </GridViewColumn.CellTemplate>
        </GridViewColumn> 
        - - - - - - - - -
        - - - - - - - - -
     </GridView>
  </ListView.View>
</ListView>

我正在使用 Visual Studio 2010

dataTable 的屏幕截图,我将其用作 ItemSource 的ListView。(调试时拍摄) 在此处输入图像描述

I have populated a ComboBox inside a ListView. Screen shot is given below
enter image description here

As shown above it's displaying "M", "a", "c" instead of "Mac". Why it is separating the word into characters?

In code behind file I've written

ItemCategoryDAL itemCategoryDalObj = new ItemCategoryDAL();
            DataTable dataTable = itemCategoryDalObj.GetAllItemCategory();

            listView1.ItemsSource = dataTable.DefaultView;

And in .xaml file I've written:

<ListView  Height="148" HorizontalAlignment="Left" Margin="23,12,0,0" Name="listView1" VerticalAlignment="Top" Width="447" >
  <ListView.View>
     <GridView>
        - - - - - - - - 
        - - - - - - - -
        <GridViewColumn Header="Category Name" Width="150">
           <GridViewColumn.CellTemplate>
               <DataTemplate>
                  <ComboBox ItemsSource="{Binding Path=IC_NAME }" Width="120" />
               </DataTemplate>
           </GridViewColumn.CellTemplate>
        </GridViewColumn> 
        - - - - - - - - -
        - - - - - - - - -
     </GridView>
  </ListView.View>
</ListView>

I'm using Visual Studio 2010

Screen shot of dataTable which I used as ItemSource for the ListView.(Taken during debuging)
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ComboBox 允许用户从多个项目中进行选择。它通过迭代其 ItemsSource 中的所有项目并将每个项目添加到其 Items 中来填充自身。

您将 ItemsSource 设置为返回字符串的属性。由于可以迭代字符串,因此 ComboBox 会使用迭代时获得的项目填充自身,因此字符串“Mac”会变成项目“M”、“a”和“ c”。

这就是为什么你会看到你所看到的。问题实际上是:您期望看到什么(或者您想看到什么)以及为什么? ComboBox应该显示哪些项目?如果您希望它显示 DataTable 中出现的所有类别名称,您可以执行以下操作:

ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=ItemsSource}"

然后使用 < code>DataTemplate:

<ComboBox.ItemTemplate>
   <DataTemplate>
      <TextBlock Text="{Binding IC_Name}"/>
   </DataTemplate>
</ComboBox.ItemTemplate>

请注意,这样做可能会遇到各种意想不到的现象。例如,如果表中只有一行将“Foo”作为 IC_Name 的值,则当用户为该行选择其他值并且表更新时,值“Foo”将从所有 ComboBox 中消失,使用户无法撤消该更改。此外,如果五行包含“Foo”,则每个 ComboBox 将在其下拉列表中显示“Foo”的所有五个实例。

A ComboBox allows the user to select from multiple items. It populates itself by iterating through all of the items in its ItemsSource and adding each item to its Items.

You're setting ItemsSource to a property that returns a string. Since a string can be iterated over, the ComboBox is populating itself with the items it gets when iterating over it, so the string "Mac" turns into the items "M", "a", and "c".

That's why you're seeing what you're seeing. The question really is: what did you expect to see (or what do you want to see) and why? What items should the ComboBox be displaying? If you want it to display all of the category names that appear in the DataTable, you could do something like:

ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=ItemsSource}"

and then pull out the IC_Name column from each item using a DataTemplate:

<ComboBox.ItemTemplate>
   <DataTemplate>
      <TextBlock Text="{Binding IC_Name}"/>
   </DataTemplate>
</ComboBox.ItemTemplate>

Note that there are going to be all kinds of unexpected phenomena that you encounter by doing this. For instance, if only one row in the table has "Foo" as a value of IC_Name, the moment the user selects some other value for that row and the table gets updated, the value "Foo" will disappear from all of the ComboBoxes, making it impossible for the user to undo that change. Also, if five rows contain "Foo", each ComboBox will display all five instances of "Foo" in their dropdown.

冰魂雪魄 2024-10-25 23:14:56

绑定似乎有效,IC_NAME 存在并返回“Mac”作为字符串。这将隐式转换为具有三个条目的可枚举字符串:“M”、“a”和“c”。这就是 ComboBox 的 ItemsSource。

<ComboBox ItemsSource="{Binding Path=IC_NAME }" Width="120" />

也许应该是这样的:

<ComboBox 
  SelectedItem="{Binding Path=IC_NAME}"
  ItemsSource="{Binding Path=NameOfACollectionProperty }" 
  Width="120" />

The binding seems to work, IC_NAME exists and returns "Mac" as a string. This will implicitely converted to an enumerable of string with three entries: "M", "a" and "c". And this is then the ItemsSource of your ComboBox.

<ComboBox ItemsSource="{Binding Path=IC_NAME }" Width="120" />

Probably it should be something like:

<ComboBox 
  SelectedItem="{Binding Path=IC_NAME}"
  ItemsSource="{Binding Path=NameOfACollectionProperty }" 
  Width="120" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文