2010-11-13 65 views
0

嘿。 我在XAML中绑定遇到了一些麻烦。我有一个Player对象的列表。我希望这个列表绑定到ListBox并显示PlayerName。目前,列表框正在填充Red.Player(即对象类型和名称空间)。我的资源字典的风格是这样的:绑定显示类型而非值

<Style x:Key="PlayerListBox" TargetType="ListBoxItem"> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="BorderThickness" Value="0"/> 
     <Setter Property="BorderBrush" Value="Transparent"/> 
     <Setter Property="Padding" Value="0"/> 
     <Setter Property="HorizontalContentAlignment" Value="Left"/> 
     <Setter Property="VerticalContentAlignment" Value="Top"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ListBoxItem"> 
        <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}" d:DesignWidth="231" d:DesignHeight="50"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"/> 
           <VisualState x:Name="MouseOver"/> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TransparentBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="SelectionStates"> 
           <VisualState x:Name="Unselected"/> 
           <VisualState x:Name="Selected"/> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <TextBlock TextWrapping="Wrap" Text="{Binding}" FontSize="29.333" TextAlignment="Center"/> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

我想主要的部分是这样的:

<TextBlock TextWrapping="Wrap" Text="{Binding}" FontSize="29.333" TextAlignment="Center"/> 

我尝试使用Text="{Binding Name}"但后来什么都没有显示出来。我设置ItemsSource当用户选择的球员:

PlayerList.ItemsSource = ListofPlayers; 

感谢所有帮助

回答

2

是名称上你已经绑定到一个类型的公共财产?这里

public class Person 
{ 
    string name; 
    int age; 

    public string Name 
    { 
     get { return name; } 
     set { name = value; } 
    } 

    public int Age 
    { 
     get { return age; } 
     set { age = value; } 
    } 

} 

示例项目演示将数据绑定到一个列表框。

binding a Linq datasource to a listbox

+0

嘿。是的,这是一个公共财产。在Player类中,我有这样的:'public string Name {get;组; }' – Skoder 2010-11-13 00:48:45

+0

D'oh,工作太久了。我刚刚拥有一个不使用私有字段的属性,所以我没有'返回名称;'。谢谢。 (我会在5分钟内接受答案:) – Skoder 2010-11-13 00:51:04

+0

很高兴为您解决。请注意,您可以在VS中使用prop片段来帮助完成此工作,而不是手动输入。 – 2010-11-13 01:32:19

0

如果你想使用{Binding}没有指定的属性,你可以覆盖的ToString()返回要显示的文字。

1

使用de ListBox.ItemTemplate属性来设置数据如何显示属性。

例如,

<ListBox.DataTemplate> 
    <DataTemplate> 
     <TextBlock Text="{Binding Name}" /> 
    </DataTemplate> 
</ListBox.DataTemplate>