我對WPF很陌生,我試圖做一些專門的數據綁定。具體來說,我有一個綁定到對象集合的DataGrid,但是我希望列的標題綁定到單獨的對象。你怎麼做到這一點?突破數據綁定層次
我有幾個像這樣定義的類:
public class CurrencyManager : INotifyPropertyChanged
{
private string primaryCurrencyName;
private List<OtherCurrency> otherCurrencies;
//I left out the Properties that expose the above 2 fields- they are the standard
//I also left out the implementation of INotifyPropertyChanged for brevity
}
public class OtherCurrency : INotifyPropertyChanged
{
private string name;
private double baseCurAmt;
private double thisCurAmt;
//I left out the Properties that expose the above 3 fields- they are the standard
//I also left out the implementation of INotifyPropertyChanged for brevity
}
然後XAML的重要部分如下所示。假設我已經將頁面綁定到了CurrencyManager類型的特定對象。請注意附加到第二個DataGridTextColumn標題的綁定是不正確的,需要以某種方式訪問CurrencyManager對象的屬性PrimaryCurrencyName。也就是說,該列的標題名稱爲「PrimaryCurrencyName」,並且列中的數據仍然綁定到其他貨幣列表的每個元素的屬性ThisCurAmt。
<DataGrid ItemsSource="{Binding Path=OtherCurrencies}" AutoGenerateColumns="False" RowHeaderWidth="0">
<DataGrid.Columns>
<DataGridTextColumn Header="Currency Name" Binding="{Binding Path=Name}"/>
<DataGridTextColumn Binding="{Binding Path=BaseCurAmt}">
<DataGridTextColumn.Header>
<Binding Path="PrimaryCurrencyName"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Header="Amt in this Currency" Binding="{Binding Path=ThisCurAmt}"/>
</DataGrid.Columns>
</DataGrid>
我該怎麼做?謝謝!
只是澄清,這是否意味着所有的項目將在第二列中具有相同的值? (也就是說,它們全部等於CurrencyManager的「primaryCurrencyName」? – ASanch 2010-08-30 19:38:52
否。primaryCurrencyName將是頭本身的名稱,而不是列中包含的數據。 – skybluecodeflier 2010-08-30 19:57:59
我明白你的意思了。希望我得到了正確的答案。 – ASanch 2010-08-30 20:09:56