嘿。 我在XAML中綁定遇到了一些麻煩。我有一個Player
對象的列表。我希望這個列表綁定到ListBox並顯示Player
的Name
。目前,列表框正在填充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;
感謝所有幫助
嘿。是的,這是一個公共財產。在Player類中,我有這樣的:'public string Name {get;組; }' – Skoder 2010-11-13 00:48:45
D'oh,工作太久了。我剛剛擁有一個不使用私有字段的屬性,所以我沒有'返回名稱;'。謝謝。 (我會在5分鐘內接受答案:) – Skoder 2010-11-13 00:51:04
很高興爲您解決。請注意,您可以在VS中使用prop片段來幫助完成此工作,而不是手動輸入。 – 2010-11-13 01:32:19