【问题标题】:WPF binding Listbox layoutpanelWPF 绑定列表框布局面板
【发布时间】:2013-08-27 16:00:31
【问题描述】:

我正在使用 devexpress,我想与 Listbox 进行绑定,但出现错误。这是我的代码:

<ListBox x:Name="_list" >                            
    <ListBox.ItemTemplate>
        <DataTemplate>
            <dxd:LayoutPanel 
                Caption="{Binding nameList}"
                AllowHide ="False" AllowFloat="False"                                            
                GotFocus="panel_GotFocus" >

                <TextBox Text="Hello" />

             </dxd:LayoutPanel>                                   

         </DataTemplate>
     </ListBox.ItemTemplate>                            
 </ListBox>    

使用此代码,Caption {Binding nameList} 为空。

我试过了。

<ListBox x:Name="_list" >                            
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                     <TextBox Text="{Binding nameList}" />
                 </Grid>                                                                      
             </DataTemplate>
         </ListBox.ItemTemplate>                            
     </ListBox> 

在这种情况下,TextBox中的文本是正确的,我需要使用第一个代码。

【问题讨论】:

  • 您至少应该在此处提及您正在使用 DevExpress。

标签: wpf binding listbox devexpress


【解决方案1】:

您似乎对如何使用这个ListBox 有点困惑。首先,您需要一个集合属性来绑定到ListBox.ItemsSource 属性。假设您有一个名为 UsersUser 对象集合:

<ListBox ItemSource="{Binding Users}" />

现在我们要定义每个User 的显示方式...User 类的所有属性都将可用于DataTemplate 内的Binding。假设User 类有两个属性; NameAge:

<DataTemplate DataType="{x:Type YourDataTypeNamespace:User}">
    <StackPanel>
        <TextBox Text="{Binding Name}" />
        <TextBox Text="{Binding Age}" />
    </StackPanel>
</DataTemplate>

然后把它们放在一起:

<ListBox ItemSource="{Binding Users}">
    <ListBox.ItemTemplate>
        <DataTemplate DataType="{x:Type YourDataTypeNamespace:User}">
            <StackPanel>
                <TextBox Text="{Binding Name}" />
                <TextBox Text="{Binding Age}" />
            </StackPanel>
        </DataTemplate>
     </ListBox.ItemTemplate>                            
 </ListBox> 

【讨论】:

  • 我已经尝试过了,但它不起作用,我认为问题出在 layoutPanel 因为它是一个 devexpress 对象并且功能不同。如果我使用 或其他本机对象,它正在工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-16
  • 2011-08-28
  • 1970-01-01
  • 2016-09-13
  • 2014-09-29
  • 2020-06-03
  • 2010-11-24
相关资源
最近更新 更多