2010-11-13 37 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>
相關問題