2011-07-15 21 views
0

我希望我對一個概念,半封閉到這裏...WPF:試圖在創建時提供動態數據

我想調用者(非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 constructur(同原始樣品):

// 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 [email protected]}"/> 
      </DataTemplate> 
      <!-- Amount item template --> 
      <DataTemplate x:Key="amountItemTemplate"> 
       <Label Content="{Binding [email protected]}"/> 
      </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 [email protected]}"></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 [email protected]}"></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 [email protected]}" /> 
         <DataGridTextColumn Header="Amount" Binding="{Binding [email protected]}" /> 
        </DataGrid.Columns> 

       </DataGrid> 

      </Grid> 
     </Grid> 

    </Grid> 

</Page> 
+0

切勿在構造函數中設置控件自己的'DataContext',這可能會導致非明顯的綁定錯誤。 – Snowbear

+0

哇。真?該代碼片段未經編輯的原始msdn示例。再次,這不會是我第一次看到演示中顯示的不好實踐。 – Jahmic

+0

從控件構造函數中設置'this.DataContext'使得從外部很容易綁定這個控件的屬性。例如在你的情況下,如果你要編寫'''''''''''''我期望這個'SomePath'可以在ExpenseReportPage的父控件的'DataContext'中搜索,你的'DataContext'考慮在內。如果你確實需要爲你的控件設置一些'DataContext',那麼將它設置在下面的一個級別。通常所有的控件都有一些'layoutRoot'控件,其中一切都嵌套。 – Snowbear

回答

0

我從一個新的演示樣本開始,事情現在工作正常。這是sample。這似乎更直接,我想做的事情。不確定工作和非工作樣品的主要區別。也許是'base.DataContext'設置。不過,隨着事情變得越來越複雜,我可能會越來越多地使用MVVM設計模式。

3

您還沒有列入您的視圖代碼,但錯在這裏有幾件事情。首先,你不能綁定到字段,所以你應該改變所有的公有字段爲屬性。此外,如果您希望在代碼中更改這些值,並在UI中反映這些更改,則應該實施INotifyPropertyChanged。此外,你應該看看MVVM design pattern有很多好處。

+0

啊,當然,屬性而不是字段。我的錯。其餘的看起來很有希望...會讓你知道它是如何結果的。 – Jahmic