我希望我對一個概念,半封閉到這裏...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>
切勿在構造函數中設置控件自己的'DataContext',這可能會導致非明顯的綁定錯誤。 – Snowbear
哇。真?該代碼片段未經編輯的原始msdn示例。再次,這不會是我第一次看到演示中顯示的不好實踐。 – Jahmic
從控件構造函數中設置'this.DataContext'使得從外部很容易綁定這個控件的屬性。例如在你的情況下,如果你要編寫'''''''''''''我期望這個'SomePath'可以在ExpenseReportPage的父控件的'DataContext'中搜索,你的'DataContext'考慮在內。如果你確實需要爲你的控件設置一些'DataContext',那麼將它設置在下面的一個級別。通常所有的控件都有一些'layoutRoot'控件,其中一切都嵌套。 –
Snowbear