WPF:尝试在创建时提供动态数据

时间:2011-07-15 09:48:02

标签: c# wpf data-binding

我希望我是关于一个概念而且距离这里一半......

我希望调用者(非WPF类)为我的WPF提供要显示的数据。所以,显然没有在XmlDataProvider和ObjectProvider中定义。

出于举例的目的,我修改了此demo。我已经引用了XmlDataProvider中的'Person'元素。相反,我做了几个非常简单的课程:

编辑:现在更改为属性(但输出没有变化):

public class ExpenseData
{
    private String expenseType;
    private String expenseAmount;

    public String ExpenseType
    {
        get { return expenseType; }
        set { expenseType = value; }
    }

    public String ExpenseAmount
    {
        get { return expenseAmount; }
        set { expenseAmount = value; }
    }
}

public class Person
{
    private String name;
    private String department;
    private ExpenseData expense;

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

    public String Department
    {
        get { return department; }
        set { department = value; }
    }

    public ExpenseData Expense
    {
        get { return expense; }
        set { expense = value; }
    }
}

然后当我调用我的WPF时,我设置了这样的数据:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ExpenseIt9.Person person = new ExpenseIt9.Person();

        person.Name = "Jimmy";
        person.Department = "Sales";
        person.Expense = new ExpenseIt9.ExpenseData();
        person.Expense.ExpenseAmount = "50";
        person.Expense.ExpenseType = "Travel";
        // View Expense Report
        ExpenseReportPage expenseReportPage = new ExpenseReportPage(person);
        this.NavigationService.Navigate(expenseReportPage);
    }

注意WPF构造(与原始样本相同)可能很重要:

// Custom constructor to pass expense report data
public ExpenseReportPage(object data):this()
{
    // Bind to expense report data.
    this.DataContext = data;
}

运行此操作后,我的数据都不会显示出来。我错过了什么?

编辑:这是视图(与原始视图相同):

    <Page x:Class="ExpenseIt.ExpenseReportPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="350" d:DesignWidth="500"
    Title="ExpenseIt - View Expense Report">

    <Grid>

        <!--Templates to display expense report data-->
        <Grid.Resources>
            <!-- Reason item template -->
            <DataTemplate x:Key="typeItemTemplate">
                <Label Content="{Binding XPath=@ExpenseType}"/>
            </DataTemplate>
            <!-- Amount item template -->
            <DataTemplate x:Key="amountItemTemplate">
                <Label Content="{Binding XPath=@ExpenseAmount}"/>
            </DataTemplate>
        </Grid.Resources>


        <Grid.Background>
            <ImageBrush ImageSource="watermark.png" />
        </Grid.Background>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="230" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>


        <Label Grid.Column="1" Style="{StaticResource headerTextStyle}">
            Expense Report For:
        </Label>
        <Grid Margin="10" Grid.Column="1" Grid.Row="1">

            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition />
            </Grid.RowDefinitions> 


            <!-- Name -->
            <StackPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" Orientation="Horizontal">
                <Label Style="{StaticResource labelStyle}">Name:</Label>
                <Label Style="{StaticResource labelStyle}" Content="{Binding XPath=@Name}"></Label>
            </StackPanel>

            <!-- Department -->
            <StackPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Orientation="Horizontal">
                <Label Style="{StaticResource labelStyle}">Department:</Label>
                <Label Style="{StaticResource labelStyle}" Content="{Binding XPath=@Department}"></Label>
            </StackPanel>

            <Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" VerticalAlignment="Top" HorizontalAlignment="Left">



                <!-- Expense type and Amount table -->
                <DataGrid ItemsSource="{Binding XPath=Expense}" ColumnHeaderStyle="{StaticResource columnHeaderStyle}" AutoGenerateColumns="False" RowHeaderWidth="0" >

                    <DataGrid.Columns>
                        <DataGridTextColumn Header="ExpenseType" Binding="{Binding XPath=@ExpenseType}"  />
                        <DataGridTextColumn Header="Amount" Binding="{Binding XPath=@ExpenseAmount}" />
                    </DataGrid.Columns>

                </DataGrid>

            </Grid>
        </Grid>

    </Grid>

</Page>

2 个答案:

答案 0 :(得分:3)

您尚未包含观看代码,但此处出现的问题很少。首先,您无法绑定到字段,因此您应该将所有公共字段更改为属性。此外,如果您希望在代码中更改这些值并在UI中反映更改,那么您应该实现INotifyPropertyChanged。此外,您应该查看MVVM design pattern以获得许多好处。

答案 1 :(得分:0)

我从一个新的演示样本开始,现在工作正常。这是sample。这似乎比我试图做的更直接。不确定工作和非工作样品的主要差异。也许是'base.DataContext'设置。

然而,随着事情变得越来越复杂,我可能会越来越多地使用MVVM设计模式。