繁体   English   中英

WPF C#数据绑定列表

[英]WPF C# DataBinding List

我正在尝试使用WPF通过数据绑定为Visual C#中的列表框创建自定义样式。

我有以下课程:

public class Page
{
    public readonly DateTime Sent;
    public readonly string PagerID;
    public readonly string Message;
    public readonly string Status;

    private Page(DateTime sent, string pagerID, string message, string status)
    {
        Sent = sent;
        PagerID = pagerID;
        Message = message;
        Status = status;
    }

    public static List<Page> GetPages()
    {
        ...
    }

    public override string ToString()
    {
        return Sent + ": " + PagerID + " - " + Message;
    }
}

和以下WPF .xaml:

<Window x:Class="KentBox.utilities.RecentPages"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:KentBox.utilities"
        Title="RecentPages" Height="640" Width="480">

    <Window.DataContext>
        <ObjectDataProvider x:Name="Provider" ObjectType="{x:Type local:Page}" MethodName="GetRecentPages" />
    </Window.DataContext>

    <Window.Resources>
        <Style TargetType="{x:Type ListBox}">
            <Setter Property="ItemTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Border>
                            <TextBlock Text="{Binding Message}" HorizontalAlignment="Stretch"></TextBlock>
                        </Border>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <ListBox ItemsSource="{Binding}" />
</Window>

如果我将TextBlock Text更改为=“ {Binding}”而不是{Binding Message},则可以使页面显示带有覆盖的ToString()内容。

我不知道如何使绑定仅显示页面的消息部分。 Visual Studio告诉我

无法在类型为“ System.Windows.Data.ObjectDataProvider”的数据上下文中解析属性“ Message”

我不知道如何更改所述数据上下文的类型。 有人可以指出我正确的方向吗?

邮件必须是公共财产。 它需要得到。

public string Message { get; private set; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM