WPF中的编号列表框 [英] Numbered listbox in WPF

查看:79
本文介绍了WPF中的编号列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在WPF中迈出第一步.我想在列表框中显示子表的数据.(与父子相关的数据表)应用程序的这一部分非常简单(即使对于我来说,对于初学者而言),但是当我真正变得太复杂的时候尝试在显示的列表框项目之前显示一个数字.因此,假设对于某个父数据行,有五个子数据行,则列表框(子数据)中的项应从1到5编号.我想到了该列表框的数据模板中有两个文本框.一个是数字,第二个是商品本身.
一种可能的方法是在"numberTextbox"的绑定中使用值转换器.但是不幸的是,在Web中只有很少的带有行号的valueconverters的代码示例.我能找到的几个示例是用C#编写的...(我对Visual Basic感到更自在)
有人可以帮我吗?

谢谢,
luccingolo

Hi,
I''m making my first steps in WPF. I would like to display the data of a childtable in a listbox.(parent-child related datatables) That part of the application is quite easy (even for me, as a beginner) But where things get really too complicated for me is when I try to display a nurmber just before the displayed listbox items. So let''s say that if for a certain parent datarow there are five child-datarows, then the items in the listbox (childdata) should be numbered from 1 to 5. I thought of two textboxes in a datatemplate for the listbox. One for the number, and the second one for the item itself.
One possible way could be to work with a valueconverter in the binding of the "numberTextbox". But unfortunately there are only very few codeexamples of valueconverters with rownumbers in teh web. And the few examples I could find were coded in C#... (I feel more at ease with visual basic)
Could anybody help me with that please?

Thanks,
luccingolo

推荐答案

我是通过创建具有调整后的ItemContainerStyle的自定义ListBox样式来实现的.
I do this by creating a custom ListBox style with an adjusted ItemContainerStyle.

<Style x:Key="FMListBox" TargetType="ListBox">
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="ListBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <Grid x:Name="PART_ItemGrid" >
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <TextBlock x:Name="PART_ItemNumber" Grid.Column="0" 
                                           Text={Binding Path=ItemNumber}" Margin="5" />
                                <TextBlock x:Name="PART_ItemText" Grid.Column="1" 
                                           Text="{Binding Path=Text}" 
                                           Margin="5" TextWrapping="Wrap" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>



您的集合对象将必须具有两个绑定属性ItemNumberText,以使上面的代码起作用



Your collection object will have to have the two bound properties ItemNumber and Text to make the code above work


约翰,
感谢您的快速答复.
但是对我来说,编码的困难部分更多的是数据访问,而不是代码的xaml部分.
就我而言,我有两个相关的表.正如我已经提到的,编号列表框"应绑定到子表.在我的WPF项目的实际版本中,我有一个"ViewModel-Class",其中包含MasterCollection和ChildView属性,两者均为ICollectionView-Type.
在您的方案"中,具有"ItemNumber"和"Text"属性. "DataAccess-Class"将是什么样子.因为当当前Masterdatarow更改时,相关的childdatarows(具有ItemNumber和Text-properties)也必须更改.知道数据是从SQL数据库加载的,并且可以通过应用程序中的实体模型访问",那么您将如何做.

谢谢
luccingolo
Hi John,
thank you for your really fast answer.
But for me the difficult part of the coding is more the data-access than the xaml-part of the code.
In my case I have two related tables. As I already mentioned, the "numbered listbox" should be bound to the childtable. In the actual version of my WPF-project I have a "ViewModel-Class" with a Master- and a ChildView-property, both of ICollectionView-Type.
In your "scenario", with the "ItemNumber" and the "Text"-property. What would the "DataAccess-Class" look like. Because when the current Masterdatarow changes, the related childdatarows (with the ItemNumber and Text-properties) would have to change too. How would you do that, knowing that the Data is loaded from a SQL-Database, and is "accessed" through an Entity-Model in the application.

Thank you,
luccingolo


不确定我是否正确理解了这个问题,但是如果您要合并两个单独的数据表,则可以选择使用LINQ进行Perharps.这样的东西(伪):
Not sure if I understood the question correctly, but if you have two separate datatables you want to combine, perharps using LINQ would be an option. Something like this (pseudo):
int counter = 0;
var list = from item1 in datatable1.AsEnumerable()
           join item2 in datatable2.AsEnumerable()
           on item1.Fields<int>("ParentPK") equals item2.Fields<int>("ChildFK")
           orderby ...
           select new {
              Orderer = counter++,
              Field1 = item1.Fields<string>("Something"),
              Field2 = item2.Fields&lt;string&gt;("Else"),</int></int>

,...
}

, ...
}


这篇关于WPF中的编号列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆