【问题标题】:Binding showing type rather than value绑定显示类型而不是值
【发布时间】:2010-11-13 00:41:16
【问题描述】:

嘿。 我在 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;

感谢您的帮助

【问题讨论】:

    标签: c# silverlight data-binding xaml windows-phone-7


    【解决方案1】:

    Name 是您绑定的类型的公共属性吗?

    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

    【讨论】:

    • 嘿。是的,它是公共财产。在 Player 类中,我有这个:public string Name { get; set; }
    • 天啊,工作太久了。我只是有一个没有使用私有字段的属性,所以我没有return name;。谢谢。 (我会在 5 分钟内接受答案,如果可以的话:)
    • 很高兴为您解决。请注意,您可以在 VS 中使用 prop sn-ps 来帮助解决这些问题,而不是手动输入。
    【解决方案2】:

    使用 de ListBox.ItemTemplate 属性设置数据相对于属性的显示方式。

    例如。

    数据模板>
    

    【讨论】:

      【解决方案3】:

      如果你想在不指定属性的情况下使用{Binding},你可以重写 ToString() 来返回你想要显示的文本。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-30
        • 2013-04-02
        • 2016-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多