查看TextBox
的繼承層次結構,我發現TextBox
沒有DisplayMemberPath
屬性。這是有道理的,因爲它沒有要綁定的ItemsSource
的項目,因此沒有DisplayMemberPath
。然而,我需要一個像DisplayMemberPath
這樣的TextBox
或其他可編輯的東西,我正在尋找基於我下面的情況的選項。尋找替代DisplayMemberPath的文本框或代碼變通辦法
類/瀏覽/的ViewModels我一起工作:
Test.cs
- 代碼第一實體
public class Test : BaseModel
{
public int Id { get; set; }
public int GasProfileId { get; set; }
}
GasProfile.cs
- 代碼第一實體
public class GasProfile : BaseModel
{
public int Id { get; set; }
public double H2O { get; set; }
public double CO2 { get; set; }
private ObservableCollection<Test> _tests;
public virtual ObservableCollection<Test> Tests
{
get { return _tests; }
set
{
if (_tests != value)
{
_tests = value;
Notify("Tests");
}
}
}
}
TestGasView.xaml
- 視圖(用戶控件)
這是TestGasView.xaml 貌似現在:
<Grid>
<ListBox ItemsSource="{Binding SelectedTechnician.Test}"
DisplayMemberPath="TestName">
selectedItem="SelectedTest"
</ListBox>
<Label Content="H20"/>
<TextBox></TextBox>
<Label Content="CO2"/>
<TextBox></TextBox>
</Grid>
這是我想它看起來像/行爲像(警告:斷碼/僞代碼):
<Grid>
<ListBox ItemsSource="{Binding SelectedTechnician.Test}"
DisplayMemberPath="TestName"
SelectedItem="SelectedTest">
</ListBox>
<Label Content="H20"/>
<TextBox Text="{Binding SelectedTest.GasProfile}"
DiaplayMemberPath="H20"></TextBox>
<Label Content="CO2"/>
<TextBox Text="{Binding SelectedTest.GasProfile}"
DiaplayMemberPath="CO2"></TextBox>
</Grid>
TestGasViewModel.cs
- 視圖模型
有一個屬性來獲取/設置SelectedTest。
正如你所看到的,我想要的TextBox
es上的數據綁定是錯誤的,因爲DisplayMemberPath
的位置是錯誤的,但這表明我非常想讓顯示的值爲1)可編輯2)顯示通過點擊測試並綁定到GasProfile上的「gas property」,SelectedTest有一個關鍵。
所以我的問題是:「我在TextBox
上有沒有一些屬性可以讓我將Text
屬性綁定到GasProfile並獲取我想要顯示的氣體的值?如果不是這樣,也許有一些方法來創建一個代理類,這將使結合是這樣的:
<Grid>
<ListBox ItemsSource="{Binding SelectedTechnician.Test}"
DisplayMemberPath="TestName"
SelectedItem="SelectedTest">
</ListBox>
<Label Content="H20"/>
<TextBox Text="{Binding SelectedTestGasProfile.H20}"></TextBox>
<Label Content="CO2"/>
<TextBox Text="{Binding SelectedTestGasProfile.CO2}"></TextBox>
</Grid>
如果它不是一個「哦,使用該屬性爲TextBox
」然後我找代碼建議使上述事情發生。
感謝
編輯
下面是截圖,以證明我確實也有一個選擇的測試和FieldGasProfileID(這是我的意思是由GasProfile)具有值:
但我認爲你不能做「Class.Property.Property」。糾正我,如果我錯了。 – 2013-03-07 18:02:08
我很確定你可以。 – Maverik 2013-03-07 18:03:14
你可以做到這一點。 – ethicallogics 2013-03-07 18:03:54