2013-08-27 98 views
-1

我正在使用devexpress,我想用ListBox做一个绑定,但是我有一个错误。这里我的代码:WPF绑定Listbox layoutpanel

<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中的文本是正确的,我需要使用第一个代码。

+0

你至少应该提到你在这里使用的DevExpress。 –

回答

0

您似乎对如何使用这个ListBox有点困惑。首先,您需要一个集合属性来绑定到ListBox.ItemsSource属性。比方说,你有User对象称为Users的集合:

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

现在,我们要定义每个User应该如何显示...的User类的所有属性将提供给DataTemplateBinding。假设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> 
+0

我试过了,但它不起作用,我认为问题是layoutPanel,因为它是一个devexpress对象,并且其功能是不同的。如果我使用或其他本地对象,它正在工作。 – user1253414

相关问题